The fix is
constexpr int foo(const int v) { auto x = bar(v); return v; }
The constexpr keyword means two very different things. The constexpr constant must be evaluated at compile time, while the constexpr function must be available for evaluation at compile time. There is nothing to stop you from calling foo at runtime. It means...
- Argument v is not necessarily constexpr.
- When bar is called with b, the answer may not be constexpr.
- The result cannot be stored in the constexpr variable, since it cannot be constexpr.
If foo is called at compile time, then x is not saved, this is a temporary variable inside the compiler, so making it constexpr makes no sense.
Consistency x can only make sense if foo is evaluated at runtime, in which case it cannot be constexpr, in which cases an error occurs.
source share