自
https://stackoverflow.com/questions/11522399/constexpr-initializing-static-member-using-static-function:The most likely reason for this is that constexpr variables have to be available as compile-time constant expressions from inside the bodies of member functions, so the variable initializers are completely defined before the function bodies -- which means the function is still incomplete (undefined) in the context of the initializer, and then this rule kicks in, making the expression not be a constant expression.
大致意思就是,由于 constexpr 必须是 compile-time known 的,所以它们的 initializer 定义位于它们所在的 function/class 之外。因此,你的代码等价于:
```c++
constexpr int TestClass1::y = initializer_for_y();
class TestClass1{ public:
static constexpr int getInt(){return 0;};
static constexpr int y;//why it's wrong?
TestClass1(){}
};
```
但是在 initializer_for_y() 试着调用 getInt() 的时候 TestClass1 尚未定义。这也就是为什么编译器会报告:
note: undefined function 'getInt' cannot be used in a constant expression