Please help me understand this syntax (implementing static assert in C++)

后端 未结 4 1338
粉色の甜心
粉色の甜心 2021-01-02 20:05

This syntax was used as a part of an answer to this question:

template 
struct static_assert;

template <>
struct static_assert         


        
4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-02 20:46

    STATIC_ASSERT(true);
    

    indeed means

    static_assert();
    

    which evaluates to nothing. static_assert is just an empty structure without any members. static_assert() creates an object of that structure and does not store it anywhere.

    This simply compiles and does nothing.

    On the other hand

    STATIC_ASSERT(false);
    

    means

    static_assert();
    

    which results in compilation error. static_assert has no specialization for false. So a general form is used. But the general form is given as follows:

    template 
    struct static_assert;
    

    which is just a declaration of a structure and not its definition. So static_assert() causes compilation error as it tries to make an object of a structure which is not defined.

提交回复
热议问题