C++ Logo


Advanced search

Re: Public member lambdas ( auto-type and designated arguments )

From: Cleiton Santoia <cleitonsantoia_at_[hidden]>
Date: Sun, 28 Nov 2021 20:51:24 -0300
Em sex., 26 de nov. de 2021 às 21:51, Jason McKesson via Std-Proposals <
std-proposals_at_[hidden]> escreveu:

> If you're going to propose something, there should probably be a
> reason for it. You haven't explained what problems you're trying to
> solve here.

Sorry, the motivation is the same as any "named tuples" proposal discussion
<https://groups.google.com/a/isocpp.org/g/std-proposals/c/N-kIXNrkTUk> or this
discussion <https://news.ycombinator.com/item?id=10827072> and some of
"named arguments" or "designated arguments", like the last example from
<https://pdimov.github.io/blog/2020/09/07/named-parameters-in-c20/>, or
1:04s in D2288 <https://youtu.be/Iflby7sSbeE?t=64>.

Mostly it is about "more clear" function calls, and a handy shortcut for
aggregate local classes.

set_color( [red=200, green=10, blue=100] );
vector<int> x ( [capacity=20, fill=15] );
socket.open ( [port=2000, ip="localhost", timeout=1000] );// <- much
better than open( 2000, "localhost", 1000 )

auto calculate(const vector<float>& v ) {
   return [count, sum, mean=sum/count];
auto x = calculate(data);
cout << x.sum; // x has count, sum and mean members without the need
to define the class.

It uses square brackets, but the main C++ syntax that represents a sequence
> of
values is curly braces. Indeed, when it comes to multiple values,
> square brackets are used to decompose sequences of values, not to
> create them.

Well, [] are used for other things, like indexing and attributes
besides binding. I don´t think there is such "concept" of decompose with []
and compose with {}

> Using a lambda makes absolutely no sense here. Lambdas are callable
> function objects; capturing values is simply a means to the end of
> making that function useful.

Kinda does, because this is the way c++ expands a lambda into a local class
today, however the members are private, ( I don't think that is even
mandated by the standard to be like that ) and making them public will
probably be easy...

The biggest difference between this and designated initializers
<https://en.cppreference.com/w/cpp/language/aggregate_initialization> in
function calls, is that you don't need to pre-define the class that you
will use as a parameter.


Received on 2021-11-28 17:51:37