C++ Logo

std-discussion

Advanced search

Idea: lambda capture list as ad-hoc structure or named members tuple

From: Raymund Hofmann <hofmannraymund_at_[hidden]>
Date: Fri, 24 Jan 2020 13:22:51 +0700
A template definition in block scope is not allowed, but it would at least
allow for something like:

template<class M, class N>
{
  M m;
  N n;
} adhocstruct= { expression for m, expression for n};

adhocstruct.m ...

...only when this proposal comes through:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1021r5.html

In contrast, generic lambda's which are just like templates are allowed in
block scope.
And the capture list of a lambda is in fact exactly this: a ad hoc
structure with auto typed members that have a name, unfortunately only
inside the lambda scope.

So why not have something like a capture list of a lambda with outside
access of its named auto typed members?

A kind of structured binding with initializing expressions for each member?

auto f()
{
auto adhocstruct=[m=expression_m, n=expression_n];

g(adhocstruct.m+adhocstruct.n);
h(adhocstruct);

return adhocstruct;
}

auto sometransform()
{
...
return [result=expression, tolerance=expression,
moved=std::move(movedfrom)];
}

decltype(sometransform()) sometransformresultvalue;

Using tuples for that has a major drawback of not being able to give
descriptive names for members.
Using predefined structs has three major drawbacks: more typing/code (DRY),
less locality and no auto member type deduction.

Received on 2020-01-24 00:28:04