I came across what seems like a contradictory error, namely, the inability to assign the value of a function to a constexprliteral constexpr(I hope that I use the language correctly). Here is an example:
class MyClass {
public:
static constexpr int FooValue(int n) { return n + 5; }
static constexpr int Foo5 = FooValue(5);
static constexpr int Foo5Alt(void) { return FooValue(5); }
};
In GCC 4.8.4, it is field initializer is not constantmarked Foo5. Found this thread suggesting that an older version of GCC might be the culprit. So I connected it to Coliru (GCC 6.2.0) and got an error 'static constexpr int MyClass::FooValue(int)' called in a constant expression before its definition is complete. I added Foo5Alt(), which returns its value as a function constexpr, not a literal, and this compiles fine.
I think I do not understand why FooValue(5)it cannot be used as an initializer for Foo5. Definition for FooValue(int n)completed, right? { return n + 5; }is the whole definition. constexprdenotes an expression that can be fully evaluated at compile time, so why can't it be used to determine the return value of a literal constexpr?
What subtlety is C ++ missing?
source
share