Lambdas in unevaluated contexts: a proposal

156 views
Skip to first unread message

Louis Dionne

unread,
Mar 7, 2016, 11:13:07 AM3/7/16
to ISO C++ Standard - Future Proposals
Hi,

I have previously raised the issue of lambdas in unevaluated contexts on this list, and the
conclusion seemed to be that the restriction was now obsolete. Hence, I have written a
proposal to rectify the current situation.

I attached the proposal to this message; it is only a draft and I welcome all constructive
comments. My hope is that the paper can be discussed at Oulu, and perhaps even make
it into C++17 in-extremis, given its small scope.

Regards,
Louis Dionne

Lambdas in unevaluated contexts.pdf

David Krauss

unread,
Mar 7, 2016, 12:19:15 PM3/7/16
to std-pr...@isocpp.org

Indeed, one could just as well try to write the following, only to be puzzled by a compiler error:

std::vector<int> v{1,2,3,4};
using Iterator = decltype(std::find_if(begin(v), end(v), [](int i) {
  return i % 2 == 0;
}));

While this is a valid use case, it is expected that using decltype on such a complex expression is less frequent outside the realm of heterogeneous computations. 


This isn’t impossible because of the redundant restriction, it’s impossible because each lambda expression has a unique type. You’ll never be able to produce another expression that can use that iterator. Lambda closures are never default constructible, so even decltype([]{}) is useless. See [expr.prim.lambda] §5.1.2/20.

The utility of lambdas in unevaluated contexts is that, given constexpr lambdas, they can be called.

It might be a good idea to preserve some restriction against a decltype specifier having a type “depending” on an embedded lambda closure. It’s my bedtime right now and I don’t suppose it would be easy to specify, though.

David Krauss

unread,
Mar 7, 2016, 12:22:37 PM3/7/16
to std-pr...@isocpp.org

On 2016–03–08, at 1:19 AM, David Krauss <pot...@gmail.com> wrote:

It might be a good idea to preserve some restriction against a decltype specifier having a type “depending” on an embedded lambda closure. It’s my bedtime right now and I don’t suppose it would be easy to specify, though.

Sorry, that example doesn’t depend on the lambda type. Guess it’s actually after my bedtime :D .

Nevertheless, it might be more user-friendly to preserve a lighter restriction.

Morwenn

unread,
Mar 7, 2016, 4:03:41 PM3/7/16
to ISO C++ Standard - Future Proposals
A few times I wished I could use lambdas in decltype to find the return type of an ADL-found function:

using whatever_t = decltype([]{ using std::abs; return abs(foo); }());

It's not a clean way to do that, but the current solution of using an adl_detail namespace to perform the same kind of trick isn't clan either.

Louis Dionne

unread,
Mar 7, 2016, 4:11:16 PM3/7/16
to ISO C++ Standard - Future Proposals
Interesting use case. This definitely falls into the "creative use cases I'm not aware of" bucket.
Reply all
Reply to author
Forward
Message has been deleted
0 new messages