C++ 11 - 无法使用constexpr函数定义constexpr文字?

Dav*_*d R 4 c++ gcc constexpr c++11

我遇到了一个看似违反直觉的错误,即无法将constexpr函数的值赋给constexpr文字(希望我正在使用正确的语言).这是一个例子:

class MyClass {
 public:
  static constexpr int FooValue(int n) { return n + 5; }
  static constexpr int Foo5 = FooValue(5);  // compiler error
  static constexpr int Foo5Alt(void) { return FooValue(5); }  // OK
};
Run Code Online (Sandbox Code Playgroud)

在GCC 4.8.4中,Foo5标记为field initializer is not constant.发现这个帖子暗示旧版本的GCC可能是罪魁祸首.所以我把它插入Coliru(GCC 6.2.0)并得到了错误'static constexpr int MyClass::FooValue(int)' called in a constant expression before its definition is complete.我添加Foo5Alt()了将其值作为constexpr函数返回而不是文字,并且编译得很好.

我想我不遵循为什么FooValue(5)不能用作初始化器Foo5.定义FooValue(int n)是完整的,不是吗? { return n + 5; }是整个定义. constexpr表示可以在编译时完全评估的表达式,那么为什么不能用它来定义constexpr文字的返回值?

我错过了C++的微妙之处?

rus*_*tyx 5

在C++中,只有在类的声明完成后才会解析类的成员函数的内联定义.

因此,即使编译器"知道" MyClass::FooValue(int),它还没有"看到"它的定义,因此它不能用在constexpr表达式中.

一般的解决方法是坚持使用constexpr成员函数,或constexpr在类外声明常量.