开发者

decltype in static_assert

开发者 https://www.devze.com 2023-02-12 05:03 出处:网络
Why this (static_assert) in a definition of a class doesn\'t work? template<class IntT, IntT low = IntT(), IntT high = IntT()>

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 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消