Lambda parameters must have names?

19 views
Skip to first unread message

Scott Meyers

unread,
May 22, 2009, 4:08:21 AM5/22/09
to
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...@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]

daniel....@googlemail.com

unread,
May 22, 2009, 1:35:54 PM5/22/09
to
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

restor

unread,
May 22, 2009, 1:38:27 PM5/22/09
to
> 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

Reply all
Reply to author
Forward
0 new messages