I\'ve run into what seems a counterintuitive error, namely, the inability to assign the value of a constexpr
function to a constexpr
literal (hope
In C++, inline definitions of member functions for a class are only parsed after the declaration of the class is complete.
So even though the compiler "knows" about MyClass::FooValue(int)
, it hasn't "seen" its definition yet, and hence it can't be used in a constexpr
expression.
A general workaround for this is to stick to constexpr
member functions, or declare constexpr
constants outside the class.
According to the standard, MyClass
is considered an incomplete type when you try to invoke FooValue
to initialize Foo5
. Therefore, you cannot use its members as you did.
The type is considered a completely-defined object type (or complete type) at the closing }
.
On the other side, the class is regarded as complete within function bodies. That's why Foo5Alt
compiles just fine.
See [class.mem]/6 for further details.