简体   繁体   English

g ++抱怨constexpr函数不是常量表达式

[英]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.

相关问题 constexpr表达式和变量生命周期,g ++和clang不同意的例子 - constexpr expression and variable lifetime, an example where g++ and clang disagree constexpr - 函数不能用于常量表达式 - constexpr - function cannot be used in a constant expression 什么决定了 constexpr function 是否是常量表达式? - What determines whether a constexpr function is a constant expression? 返回常量表达式不需要constexpr函数吗? - A constexpr function is not required to return a constant expression? 从constexpr函数返回类需要使用g ++虚拟关键字 - Returning a class from a constexpr function requires virtual keyword with g++ constexpr静态模板函数:g ++错误是对clang的警告 - constexpr static template function: g++ error is a warning on clang g ++不会使用assert编译constexpr函数 - g++ doesn't compile constexpr function with assert in it 错误“无法在常量表达式中出现”在g ++中,但在gcc中不存在 - error “cannot appear in a constant-expression” in g++ but not in gcc g++ 抱怨“虚拟常量……不能重载” - g++ complains that “virtual const … cannot be overloaded” 永远不会计算为常量表达式的 lambda() 可以是 C++ 中的 `constexpr` 函数吗? - Can lambda() that never evaluates to a constant expression be a `constexpr`-function in C++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM