![](/img/trans.png)
[英]constexpr expression and variable lifetime, an example where g++ and clang disagree
[英]g++ complains constexpr function is not a constant expression
我已將問題減少到以下幾點:
struct A {
static constexpr std::size_t f() { return 4; }
};
template<std::size_t N>
struct B : A {
alignas(A::f()) char a[N];
};
我沒有看到這有什么問題,但如果我嘗試使用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
我不知道為什么原始代碼不好但這是一個解決方法:
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.