Topic: Types of arguments in lambda expressions proposal


Author: Mathias Gaunard <loufoque@gmail.com>
Date: Tue, 3 Jul 2007 08:10:13 CST
Raw View
In the current lambda expressions proposal (N2329), it is said it is
possible to write parameters without their types:
<>(x, y) { x + y }

However, it doesn't clearly state how x and y are passed to the
generated functor.
Is it by value? By reference? Const-reference? Rvalue-reference?

The most reasonable choice to me seems to be by rvalue-reference,
because the type may not be copiable, or could be expensive to copy.
Plus lambda expressions are usually short enough to be good candidates
for inlining.

However, some parts of the proposal, which use ints as examples, make
me think that it is pass-by-value that is intended here. While it may
be a good idea for ints, it certainly is not for non-trivial types,
and those are what the lambda expressions will be working with most of
the time.

How exactly are x and y passed here? What is the intent of the
proposal?

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: jarvij@gmail.com
Date: Tue, 3 Jul 2007 22:52:01 CST
Raw View
On 3 Jul, 09:10, Mathias Gaunard <loufo...@gmail.com> wrote:

Hello Mathias,

> In the current lambda expressions proposal (N2329), it is said it is
> possible to write parameters without their types:
> <>(x, y) { x + y }
>
> However, it doesn't clearly state how x and y are passed to the
> generated functor.
> Is it by value? By reference? Const-reference? Rvalue-reference?
>

Section 6 attempts to explain. Quoting the paper:

2. There are three different operations that a lambda function can be
subjected to after its definition: it can be
called, its type can be bound to a constrained template parameter, or
its type can be bound to an unconstrained
template parameter. We discuss each of those in turn:

(a) Calling a lambda function.

When compiling an invocation to a lambda function, argument types to
it
become known. Consider the following call:
int prod = 1;
auto cumul product = <>(x : auto p& = prod)
{ p  = x }
..
cumul product(5);
The type of the argument 5 is int, which we use as the type of the
lambda function's parameter x. We must
also select the parameter passing mode. If the type of an argument is
T, the parameter type is T&&. For
example, above, the parameter type is int&&. In the call below, the
parameter type is const int&&:

const int fact = 10;
cumul product(val);



(b) Binding a lambda function to a constrained template argument.
..
</quote>

so in case (a), parameter passing mode is &&
in case (b), however, the parameter types are obtained from some
constraint like:

T operator()(A, B, C, D);

which gives the exact parameter list. There is no need to add & or &&
or const & or such.

Best,

  Jaakko J   rvi


> The most reasonable choice to me seems to be by rvalue-reference,
> because the type may not be copiable, or could be expensive to copy.
> Plus lambda expressions are usually short enough to be good candidates
> for inlining.
>
> However, some parts of the proposal, which use ints as examples, make
> me think that it is pass-by-value that is intended here. While it may
> be a good idea for ints, it certainly is not for non-trivial types,
> and those are what the lambda expressions will be working with most of
> the time.
>
> How exactly are x and y passed here? What is the intent of the
> proposal?
>


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]