C++ 哪个编译器是对的?
以下代码在clang中正确构建++C++ 哪个编译器是对的?,c++,c++11,compiler-errors,constexpr,C++,C++11,Compiler Errors,Constexpr,以下代码在clang中正确构建++ class Number { int _value; public: constexpr Number(int value): _value(value) {} constexpr bool operator==(const Number rhs) const { return _value == rhs._value; } constexpr bool isOne() const { return *this == N
class Number
{
int _value;
public:
constexpr Number(int value): _value(value) {}
constexpr bool operator==(const Number rhs) const { return _value == rhs._value; }
constexpr bool isOne() const { return *this == Number::ONE; }
static const Number ONE;
};
constexpr Number Number::ONE(1);
int main()
{
return 0;
}
但在gcc++中失败。返回的错误如下所示:
Error(s):
266649471/source.cpp: In member function ‘constexpr bool Number::isOne() const’:
266649471/source.cpp:11:65: error: the value of ‘Number::ONE’ is not usable in a constant expression
constexpr bool isOne() const { return *this == Number::ONE; }
^
266649471/source.cpp:13:25: note: ‘Number::ONE’ was not declared ‘constexpr’
static const Number ONE;
^~~
谁在这里?是否有任何方法可以声明Number::ONE constexpr内部编号