Will I be able to declare a constexpr lambda inside a template parameter?

No, that is a compiler bug. gcc 7.1 correctly rejects the code.

[expr.prim.lambda]/2:

A lambda-expression is a prvalue whose result object is called the closure object. A lambda-expression shall not appear in an unevaluated operand, in a template-argument, in an alias-declaration, in a typedef declaration, or in the declaration of a function or function template outside its function body and default arguments.

As you can see from the part that I marked as bold, a lambda expression cannot appear in a template argument list.

This is also made clear in a subsequent note:

[ Note: The intention is to prevent lambdas from appearing in a signature. — end note ]

If I were to guess, I would say that the bug comes about because starting with C++17, lambdas are implicitly constexpr, which makes them valid to be called in compile time expressions, like template arguments. But actually defining a lambda in a template argument is still illegal.


Note that this restriction has been lifted in C++20. 🙂

Leave a Comment