19

I had this lambda somewhere in my code:

[](bool a, bool=true){ return !a;} }

and GCC 4.6 "complained" with this warning:

warning: default argument specified for lambda parameter [-pedantic]

Which is mightily unhelpful when you don't know why this is "bad". I consulted the FDIS n3290 and didn't find anything in 5.1.2 Lambda Expressions with regards to default arguments and a lambda.

UPDATE: I filed a bug report here.

UPDATE2: OK, from now on I'm using -pedantic-errors. -pedantic only emits warnings, not errors.

9
  • 1
    I love the Final Draft International Standard standard. Jun 8, 2011 at 16:12
  • 1
    @Tomalak: what a strange thing to say...
    – rubenvb
    Jun 8, 2011 at 16:42
  • @rubenvb: he's just kvetching about "PIN number" syndrome, where the word for which the last letter of an acronym stands, is appended to the acronym. This is redundant, since "PIN" already says that it's a number, and "FDIS" already says that it's a standard. Jun 8, 2011 at 17:14
  • 1
    Why did this get the [c++] tag if it's clearly only C++0x?
    – Xeo
    Jun 8, 2011 at 18:15
  • 2
    @Xeo: Because the tag-info page says so. :) Check this.
    – Vitus
    Jun 8, 2011 at 20:44

3 Answers 3

15

Section 5.1.2 paragraph 5 specifically says that you can not have default values for the parameters.

Default arguments (8.3.6) shall not be specified in the parameter-declaration-clause of a lambda-declarator.

5
  • Seems daft that this is merely a pedantic warning, rather than a flat-out error. Silly GCC. Jun 8, 2011 at 16:12
  • Dang, must've missed that. GCC should make that an error then!
    – rubenvb
    Jun 8, 2011 at 16:12
  • @Bo Persson, @rubenvb: I don't remember if compilers are supposed to diagnose per default ? I think there are so places in the standard with the dreadful "no diagnostic required", but I don't know what should be the behavior when it's not present. Jun 8, 2011 at 16:52
  • @Matthieu - compilers are of course allowed to have extensions and accept code that isn't strictly according to the standard. Compiling Windows headers by default is a good idea for some of them. :-) With gcc I guess -pedantic-errors is what it takes!
    – Bo Persson
    Jun 8, 2011 at 17:04
  • 2
    @Tomalak: the standard requires that the ill-formed program be diagnosed. gcc has diagnosed it. If you want it to fail in addition to diagnosing, use -Werror or -pedantic-errors. -pedantic is documented "issue all the warnings demanded", if you wanted errors it seems "daft" to use the option that only gives you warnings ;-p Jun 8, 2011 at 17:17
10

Since C++14 it is allowed. It was found to be a defect long time ago: Default arguments for lambdas, and also Default arguments in lambda-expressions.

1

It makes no sense to have a default argument in a lambda function -- how could it ever be used? On the other hand, it does no harm, so why not allow it, after emitting a warning?

7
  • 1
    Yes, it does harm, in making your program unportable. Jun 8, 2011 at 18:02
  • @Tamàs: I think TonyK meant that he finds it odd it isn't allowed by the Standard.
    – rubenvb
    Jun 8, 2011 at 18:19
  • I don't know, I interpreted his answer as claiming the compiler does right if it only shows a warning for nonstandard code. Unfortunately gcc/g++ does enable GNU extensions by default, which I believe it shouldn't. Jun 8, 2011 at 19:42
  • 12
    Why doesn't it make sense? I don't see the reason for this restriction, it can be useful, and there is no problem with implementation. For example, what's the fundamental problem with the original lambda: auto lambda = [](bool a, bool b = true) { return !a && b;};? You can write a closure by hand that will work just fine. Jun 8, 2011 at 20:34
  • 2
    The place where I frequently want default arguments to lambdas is when using them as simple local functions. auto addThingy = [](const std::string & name, int value, const std::string & extraSpecialText = {}) Jun 22, 2016 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.