Bug 78131

Summary: Inconsistent evaluation for `constexpr` lambdas in templates between `static_assert`, `if constexpr(…)` and `constexpr` variables
Product: gcc Reporter: Vittorio Romeo <vittorio.romeo>
Component: c++Assignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: normal CC: daniel.kruegler, victor.dyachenko, webrown.cpp
Priority: P3    
Version: 7.0   
Target Milestone: 8.0   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed:

Description Vittorio Romeo 2016-10-27 14:30:33 UTC
template <typename TF>
constexpr auto f(TF)
{
    return [](auto...) constexpr { return true; };
}   

// Compiles and works as intended.
template <typename T0>
void ok_0(T0)
{
    static_assert(f([](auto x) -> decltype(x){})(T0{}));
}

// Compiles and works as intended.
template <typename T0>
void ok_1(T0)
{
    constexpr auto a = f([](auto x) -> decltype(x){})(T0{});
    if constexpr(a) { }
}

// Compile-time error!
template <typename T0>
void fail_0(T0)
{
    if constexpr(f([](auto x) -> decltype(x){})(T0{})) { } 
}

---

Error:
prog.cc: In function 'void fail_0(T0)':
prog.cc:29:54: error: expression '<lambda>' is not a constant expression
     if constexpr(f([](auto x) -> decltype(x){})(T0{})) { }

Minimal example:
http://melpon.org/wandbox/permlink/NY4oKSlA76CkqPQ9

Related StackOverflow question:
http://stackoverflow.com/questions/40283001

P.S. the bug is still present even if 'constexpr' is explicitly specified for the lambdas passed to 'f'.
Comment 1 paolo@gcc.gnu.org 2017-10-04 20:59:24 UTC
Author: paolo
Date: Wed Oct  4 20:58:52 2017
New Revision: 253431

URL: https://gcc.gnu.org/viewcvs?rev=253431&root=gcc&view=rev
Log:
2017-10-04  Paolo Carlini  <paolo.carlini@oracle.com>

	PR c++/78131
	* g++.dg/cpp1z/constexpr-lambda17.C: New.

Added:
    trunk/gcc/testsuite/g++.dg/cpp1z/constexpr-lambda17.C
Modified:
    trunk/gcc/testsuite/ChangeLog
Comment 2 Paolo Carlini 2017-10-04 21:00:01 UTC
Fixed in trunk.