Why this (static_assert) in a definition of a class doesn't work?
template<class IntT, IntT low = IntT(), IntT high = IntT()>
struct X
{
static_assert(std::is_same<decltype(low),decltype(high)>::value,"Different types not allowed");
};
int _tmain(int argc, _TCHAR* argv[])
{
int low, high;
X<char,1,'a'> x;//HERE I SHOULD GET ERROR
cout << sizeof(x);
return 0;
}
static_assert
works fine, is your code that never assert.The template
struct X
defineslow
andhigh
as of typeIntT
. They are both the same type, whatever values they have.When you instantiate the struct (
X<char,1,'a'> x
) you are telling the compiler that the type ofIntT
ischar
and are giving tolow
the value1
and tohigh
the value'a'
(i.e. 97). However, the type oflow
andhigh
is alwayschar
so thestatic_assert
will never assert.