Bug 78131 - Inconsistent evaluation for `constexpr` lambdas in templates between `static_assert`, `if constexpr(…)` and `constexpr` variables
Summary: Inconsistent evaluation for `constexpr` lambdas in templates between `static_...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 7.0
: P3 normal
Target Milestone: 8.0
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-10-27 14:30 UTC by Vittorio Romeo
Modified: 2017-10-04 21:00 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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.