decltype in static_assert

3.1k Views Asked by At

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;
}
1

There are 1 best solutions below

0
On BEST ANSWER

static_assert works fine, is your code that never assert.
The template struct X defines low and high as of type IntT. 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 of IntT is char and are giving to low the value 1 and to high the value 'a' (i.e. 97). However, the type of low and high is always char so the static_assert will never assert.