Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lambda in branch not taken of 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: Compiles NoTemplate sucessfully; gives error: a lambda expression may not appear inside of a constant expression on YesTemplate.
  • gcc: Compiles both successfully
  • msvc: Crashes.
  • icc: Crashes (we have a winner!)

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

like image 952
Sessile Computing Avatar asked Dec 19 '17 21:12

Sessile Computing


1 Answers

This program is well-formed.

Note after C++17, a lambda expression may be accepted in a core constant expression even if it would be evaluated. You can see the proposed paper N4487 for detail.

like image 131
xskxzr Avatar answered Oct 22 '22 16:10

xskxzr