[英]G++ and STD 11 has problems with constexpr
Im trying use the same constexpr in g++ and clang++, both latest version and with param "-std=c++11". 我正在尝试在g ++和clang ++中使用相同的constexpr,包括最新版本和参数“ -std = c ++ 11”。 Clang compiles without problem, but G++ return error. Clang编译没有问题,但是G ++返回错误。 The source is: 来源是:
#include <functional>
enum class LoggerLevel : unsigned {
NO_LEVEL = 0,
VERBOSE = 1 << 0,
DEBUG = 1 << 1,
INFO = 1 << 2,
WARNING = 1 << 3,
ERROR = 1 << 4,
FATAL = 1 << 5,
ALL_LEVELS = 0 | VERBOSE | DEBUG | INFO | WARNING | ERROR | FATAL,
};
constexpr LoggerLevel operator|(LoggerLevel lhs, LoggerLevel rhs) noexcept {
return static_cast<LoggerLevel>(static_cast<unsigned>(lhs) | static_cast<unsigned>(rhs));
}
constexpr LoggerLevel& operator|=(LoggerLevel& lhs, LoggerLevel rhs) noexcept {
return lhs = lhs | rhs;
}
int main()
{
auto x = LoggerLevel::ALL_LEVELS;
return 0;
}
And the error is: 错误是:
<source>: In function 'constexpr LoggerLevel& operator|=(LoggerLevel&, LoggerLevel)':
<source>:19:16: error: expression '(lhs = operator|(lhs, rhs))' is not a constant expression
return lhs = lhs | rhs;
~~~~^~~~~~~~~~~
Compiler returned: 1
And the godbolt example: 还有一个例子
https://godbolt.org/z/M6ERms https://godbolt.org/z/M6ERms
Thanks with any help. 感谢您的帮助。
Compile for at least the C++14 standard as prior to C++14, the core constant expression evaluation would not evaluate an assignment or a compound assignment operator which is what you have in: 至少对于C ++ 14之前的C ++ 14标准进行编译,核心常量表达式求值将不会评估您所拥有的赋值或复合赋值运算符:
return lhs = lhs | rhs;
The relevant chapter in the C++11 draft is 5.19.2 Constant expressions (emphasis mine): C ++ 11草案中的相关章节是5.19.2常量表达式 (强调我的意思):
A conditional-expression is a core constant expression unless it involves one of the following as a potentially evaluated subexpression ... 条件表达式是核心常量表达式,除非它涉及以下之一作为可能评估的子表达式...
- an assignment or a compound assignment 作业或复合作业
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.