My question is, whether the following code is valid:
template<int i> class Class
{
static_assert(sizeof(i) == 0, "Class instantiated with i != 1");
};
template<> class Class<1> {};
This snippet compiles with g++. But clang++ is trapped by the static_assert:
error: static_assert failed "Class instantiated with non-int type"
A template that uses a type instead of an int like
template<typename T> class Class
{
static_assert(sizeof(T) == 0, "Class instantiated with non-int type");
};
template<> class Class<int> {};
is accepted by both compilers. Exactly the same pattern applies to function templates.
I found open-std.org::Non-dependent static_assert-declarations, but that does not seem to apply, because my static_assert is dependent on the template parameter.
You may check the described behavior on godbolt.org
EDIT: As Johan Lundberg points out in the comment my question is wrong. Indeed sizeof(i) does not depend on the template parameter. Also R.Sahu is completely right: It would make much more sense to assert i != 1. For this again both compilers accept the code.
However, still the upper example compiles with g++. As open-std.org::Non-dependent static_assert-declarations applies to that case (I apologize again for the wrong question in this respect): Is g++ actually wrong in compiling the code without error?
static_assert(i != 1, "Class instantiated with i != 1");by any chance?std::function<T>. You can also define atemplate<int N> struct always_false : public std::false_type {};and dostatic_assert(always_false<i>::value, "My error");