C ++ static_assert没有按预期失败

时间:2017-11-03 21:21:05

标签: c++ templates constructor initializer-list static-assert

我刚刚遇到以下代码,如果使用了templaterized static_assert,那么第一个pred1将不会失败,并且在main x中声明了一个空的初始化列表。这是符合标准的东西,还是编译器错误?我在Fedora 25上使用g ++ 6.4.1。

#include <cstdio>

template<class T> struct pred1 {
    constexpr static bool value = false;
};

struct pred2 {
    constexpr static bool value = false;
};

template<class A> struct align {
    // this assertion only fails with align<int> x; but not with align<int> x();
    static_assert( pred1<A>::value == true, "fail1" );

    // this assertion will fail anyway
    // static_assert( pred2::value == true, "fail2" );
};

int main() {

    align<int> x; // this fails
    align<int> x {}; // this fails too
    align<int> x(); // this does not fail

    return 0;
}

0 个答案:

没有答案