Topic: Lambda parameters must have names?


Author: daniel.kruegler@googlemail.com
Date: Fri, 22 May 2009 11:35:54 CST
Raw View
On 22 Mai, 10:08, Scott Meyers <use...@aristeia.com> wrote:
> VC10 beta complains if try to use a lambda with an unnamed parameter.
> Here's a trivial code fragment I was trying to use to play around with
> the beta's support for C++0x:
>
>  std::forward_list<int> fli;
>  std::vector<int> v;
>  std::copy_if(fli.cbegin(), fli.cend(),
>               std::back_inserter(v),
>       [](int){ return true; });
>
> In this case, my "the laziest thing that could possibly work" approach
> was to create a lambda predicate that always returns true, so the
> value of the parameter was immaterial, and I left it out.  VC10 beta
> says:
>
> vc10test.cpp(12) : error C3494: a lambda must have named parameters
>
> I didn't dig through N2800 enough to verify that this is a correct
> diagnostic, but I'm willing to give VC10 beta the benefit of the
> doubt. (Another application of the "the laziest thing that could
> possibly work", and I apologize for it here.)

VC10 strictly follows the current WP wording here, see below.

> Assuming this is a correct diagnostic, is there a reason why lambdas
> are the only place in C++ where function parameters must have names,
> even if they are not used?

This is a bug in the standard. Note that in [expr.prim.lambda] the
grammar

lambda-parameter-declaration:
   ( lambda-parameter-declaration-list_opt ) mutable_opt attribute-
specifier_opt
   exception-specification_opt lambda-return-type-clause_opt

lambda-parameter-declaration-list:
   lambda-parameter
   lambda-parameter , lambda-parameter-declaration-list

lambda-parameter:
   decl-specifier-seq attribute-specifier_opt declarator

Following the definition of /declarator/ we find that it requires
a (non-optional) declarator-id.

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2859.pdf

has fixed this by changing the grammar element definition to

lambda-expression:
   lambda-introducer lambda-declarator_opt compound-statement

lambda-declarator:
   ( parameter-declaration-clause ) attribute-specifier_opt mutable_opt
   exception-specification_opt trailing-return-type_opt

which now correctly refers to a /parameter-declaration-clause/,
which follows the normal rules of function parameter declarations
as of [dcl.fct].

Greetings from Bremen,

Daniel

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





Author: restor <akrzemi1@interia.pl>
Date: Fri, 22 May 2009 11:38:27 CST
Raw View
> Assuming this is a correct diagnostic, is there a reason why lambdas
> are the only place in C++ where function parameters must have names,
> even if they are not used?

Hi, I think this syntax is reserved for possible future addition to
the language: polymorphic lambdas as described in e.g. N2329, where a
lambda declaration like
[]( x, y ) { return x + y; }
would list parameter names rather than types.

Rgards,
&rzej



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





Author: Scott Meyers <usenet@aristeia.com>
Date: Fri, 22 May 2009 02:08:21 CST
Raw View
VC10 beta complains if try to use a lambda with an unnamed parameter.
Here's a trivial code fragment I was trying to use to play around with
the beta's support for C++0x:

 std::forward_list<int> fli;
 std::vector<int> v;
 std::copy_if(fli.cbegin(), fli.cend(),
              std::back_inserter(v),
      [](int){ return true; });

In this case, my "the laziest thing that could possibly work" approach
was to create a lambda predicate that always returns true, so the
value of the parameter was immaterial, and I left it out.  VC10 beta
says:

vc10test.cpp(12) : error C3494: a lambda must have named parameters

I didn't dig through N2800 enough to verify that this is a correct
diagnostic, but I'm willing to give VC10 beta the benefit of the
doubt. (Another application of the "the laziest thing that could
possibly work", and I apologize for it here.)

Assuming this is a correct diagnostic, is there a reason why lambdas
are the only place in C++ where function parameters must have names,
even if they are not used?

Thanks,

Scott

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