C ++ 11 - Can't define constexpr literal using constexpr function?

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);  // compiler error
  static constexpr int Foo5Alt(void) { return FooValue(5); }  // OK
};

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?

+4
source share
2 answers

In C ++, inline definitions of member functions for a class are processed only after the class declaration is complete.

, "" MyClass::FooValue(int), "" , , constexpr.

- constexpr constexpr .

+5

, MyClass , FooValue Foo5. , . ( ) }.
, . Foo5Alt .
. [class.mem]/6.

+4

Source: https://habr.com/ru/post/1663273/