The lambda in the branch is not taken from a constant expression: who is right?

I tried to compile the following C ++ 11 code with mixed results.

struct NoTemplate { static constexpr auto (*foo)() = false ? +[]{} : nullptr; }; NoTemplate no_inst; template<typename> struct YesTemplate { static constexpr auto (*foo)() = false ? +[]{} : nullptr; }; YesTemplate<float> yes_inst; 
  • clang: compile NoTemplate ; gives error: a lambda expression may not appear inside of a constant expression on YesTemplate .
  • gcc: compiles successfully
  • msvc: failed.
  • icc: Crash (we have a winner!)

What is the correct result? I see that some standard language offering non-constant expressions should be OK in the false branch of short trailing operators in constant expressions, but IANALL.

+8
c ++ language-lawyer lambda c ++ 11
source share
1 answer

This program is well formed.

Note after C ++ 17, the lambda expression can be accepted in the main constant expression, even if it is evaluated. For more information, see proposed article N4487 .

0
source share

All Articles