[英]g++ complains constexpr function is not a constant expression
I've reduced my problem to the following: 我已将问题减少到以下几点:
struct A {
static constexpr std::size_t f() { return 4; }
};
template<std::size_t N>
struct B : A {
alignas(A::f()) char a[N];
};
I don't see what's wrong with this, yet if I try to compile using g++
: 我没有看到这有什么问题,但如果我尝试使用g++
进行编译:
main.cpp:9:19: error: expression 'A::f' is not a constant-expression
alignas(A::f()) char a[N];
^
main.cpp:9: confused by earlier errors, bailing out
Reproduction is available on coliru . coliru上有复制品 。
I don't know why the original code is bad but here is a workaround: 我不知道为什么原始代码不好但这是一个解决方法:
struct A {
static constexpr std::size_t f() { return 4; }
};
template<std::size_t ALIGN, std::size_t N>
struct C {
alignas(ALIGN) char a[N];
};
template<std::size_t N>
struct B : A, C<A::f(), N> {
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.