I have a template struct which can't be used directly, only specializations are allowed. And I want to use static_assert
to show meaningful error message. I can't just type static_assert(false, "error");
since false
isn't template dependent and compiler may show error message even if the template is never used.
My solution:
template<class>
struct AlwaysFalse : std::false_type{};
#define DEPENDENT_FALSE(arg) AlwaysFalse<decltype(sizeof(arg))>::value
template<class T>
struct Foo{
static_assert(DEPENDENT_FALSE(T), "You must use specialization!");
};
template<int i>
struct Bar{
static_assert(DEPENDENT_FALSE(i), "You must use specialization!");
};
But I'm not sure about realization DEPENDENT_FALSE
. Because MSVC doesn't treat sizeof(arg)
as template dependent expression(unlike GCC), but decltype(sizeof(arg))
is fine.
Can somebody explain this behavior in terms of standard? Is it portable?
Aucun commentaire:
Enregistrer un commentaire