Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #10305 > unrolled thread

C++11 lambdas

Started byJerry <jerry.jeremiah@gmail.com>
First post2011-09-19 15:40 -0700
Last post2011-10-24 17:27 +0900
Articles 10 — 7 participants

Back to article view | Back to comp.lang.c++


Contents

  C++11 lambdas Jerry <jerry.jeremiah@gmail.com> - 2011-09-19 15:40 -0700
    Re: C++11 lambdas Lucien Coffe <lcof@nospam.invalid> - 2011-09-20 06:38 +0000
    Re: C++11 lambdas Juha Nieminen <nospam@thanks.invalid> - 2011-09-20 06:41 +0000
      Re: C++11 lambdas Miles Bader <miles@gnu.org> - 2011-09-20 16:57 +0900
      Re: C++11 lambdas Noah Roberts <roberts.noah@gmail.com> - 2011-09-20 08:23 -0700
        Re: C++11 lambdas Juha Nieminen <nospam@thanks.invalid> - 2011-09-21 07:14 +0000
          Re: C++11 lambdas Victor Bazarov <v.bazarov@comcast.invalid> - 2011-09-21 18:25 -0400
    Re: C++11 lambdas ChuanQi Tan <chuanqi.tan@gmail.com> - 2011-10-04 22:04 -0700
      Re: C++11 lambdas Jerry <jerry.jeremiah@gmail.com> - 2011-10-23 15:21 -0700
        Re: C++11 lambdas Miles Bader <miles@gnu.org> - 2011-10-24 17:27 +0900

#10305 — C++11 lambdas

FromJerry <jerry.jeremiah@gmail.com>
Date2011-09-19 15:40 -0700
SubjectC++11 lambdas
Message-ID<835cd0b9-b3e1-4aa2-bf61-f71760b8fb04@fi7g2000vbb.googlegroups.com>
This works fine:

int main()
{
    std::map<std::string,std::string> parameters;

    parameters["abc"]="def";

    std::for_each(parameters.begin(),parameters.end(),
        [] (std::map<std::string,std::string>::value_type& pair)
            {std::cout << pair.first << "=" << pair.second << " ";});

    return 0;
}

But g++ 4.6.0 doesn't like it when I change the type of pair in the
lambda to auto:

int main()
{
    std::map<std::string,std::string> parameters;

    parameters["abc"]="def";

    std::for_each(parameters.begin(),parameters.end(),
        [] (auto pair)
            {std::cout << pair.first << "=" << pair.second << " ";});

    return 0;
}

So, my question is: why can't it be auto?

In general I am most interested in why the committee chooses to
include things or not and I figure there must be a reason this isn't
allowed.  Or is it allowed and my compiler is just too old?

Thanks,

Jerry

[toc] | [next] | [standalone]


#10321

FromLucien Coffe <lcof@nospam.invalid>
Date2011-09-20 06:38 +0000
Message-ID<j59cd5$tvl$1@speranza.aioe.org>
In reply to#10305
Jerry wrote :

> This works fine:
> 
> int main()
> {
>     std::map<std::string,std::string> parameters;
> 
>     parameters["abc"]="def";
> 
>     std::for_each(parameters.begin(),parameters.end(),
>         [] (std::map<std::string,std::string>::value_type& pair)
>             {std::cout << pair.first << "=" << pair.second << " ";});
> 
>     return 0;
> }
> 
> But g++ 4.6.0 doesn't like it when I change the type of pair in the
> lambda to auto:
> 
> int main()
> {
>     std::map<std::string,std::string> parameters;
> 
>     parameters["abc"]="def";
> 
>     std::for_each(parameters.begin(),parameters.end(),
>         [] (auto pair)
>             {std::cout << pair.first << "=" << pair.second << " ";});
> 
>     return 0;
> }
> 
> So, my question is: why can't it be auto?
> 
> In general I am most interested in why the committee chooses to include
> things or not and I figure there must be a reason this isn't allowed. 
> Or is it allowed and my compiler is just too old?
> 
> Thanks,
> 
> Jerry

Auto knows its type from its initializer. Here there are no initializer. 
It is used for *convenience*. I may be wrong, but I think this is not 
possible because of this.

Here is an example of why : http://ideone.com/pP5jz
/error: parameter declared 'auto'/

-- 
perl -e 's;;{]``*%)}`_^[&)/#%(`&;;\
y;%^)([]/*#&`_{};.\100acghiklmopsz;;print'

[toc] | [prev] | [next] | [standalone]


#10322

FromJuha Nieminen <nospam@thanks.invalid>
Date2011-09-20 06:41 +0000
Message-ID<4e78359c$0$4384$7b1e8fa0@news.nbl.fi>
In reply to#10305
Jerry <jerry.jeremiah@gmail.com> wrote:
> So, my question is: why can't it be auto?

  Not (yet) a C++11 expert, but in general function parameters cannot be
of type 'auto'. The function signature would depend on the type of the
given parameter, and it would basically be a templated function in that
case.

  Of course this raises two interesting questions:

  1) Can lambdas be templatized? I honestly don't know.

  2) Why can't the compiler "auto-templatize" a function that takes a
parameter of type 'auto'? (In other words, implicitly make it a template
function where the parameter marked as 'auto' is a templated type.)
I guess this would open a pandora's box of complications, and for dubious
benefit (after all, it would just save a "template" declaration and little
else).

[toc] | [prev] | [next] | [standalone]


#10329

FromMiles Bader <miles@gnu.org>
Date2011-09-20 16:57 +0900
Message-ID<buo4o071w9a.fsf@dhlpc061.dev.necel.com>
In reply to#10322
Juha Nieminen <nospam@thanks.invalid> writes:
>   2) Why can't the compiler "auto-templatize" a function that takes a
> parameter of type 'auto'? (In other words, implicitly make it a template
> function where the parameter marked as 'auto' is a templated type.)

I seem to recall that this was considered, along with the same thing
("implicit templates") for ordinary functions, and came sort of close to
getting in, but was decided to be just a bit too much for the current
standard.  Soooo maybe in a future standard.

There's some discussion of it here (search for "polymorphic lambdas"):

http://channel9.msdn.com/Shows/Going+Deep/Herb-Sutter-C-Questions-and-Answers

-Miles

-- 
Omochiroi!

[toc] | [prev] | [next] | [standalone]


#10339

FromNoah Roberts <roberts.noah@gmail.com>
Date2011-09-20 08:23 -0700
Message-ID<c1232ecc-3aa7-48f3-b4b4-cb9975e07232@hb5g2000vbb.googlegroups.com>
In reply to#10322
On Sep 19, 11:41 pm, Juha Nieminen <nos...@thanks.invalid> wrote:
> Jerry <jerry.jerem...@gmail.com> wrote:
> > So, my question is: why can't it be auto?
>
>   Not (yet) a C++11 expert, but in general function parameters cannot be
> of type 'auto'. The function signature would depend on the type of the
> given parameter, and it would basically be a templated function in that
> case.
>
>   Of course this raises two interesting questions:
>
>   1) Can lambdas be templatized? I honestly don't know.

No.  You can create them within templates though, which could serve
much the same purpose:

template < typename T >
function<void(T const&)> printer() { return [](T const& t) { ... }; }

[toc] | [prev] | [next] | [standalone]


#10356

FromJuha Nieminen <nospam@thanks.invalid>
Date2011-09-21 07:14 +0000
Message-ID<4e798ed2$0$2835$7b1e8fa0@news.nbl.fi>
In reply to#10339
Noah Roberts <roberts.noah@gmail.com> wrote:
> No.  You can create them within templates though, which could serve
> much the same purpose:
> 
> template < typename T >
> function<void(T const&)> printer() { return [](T const& t) { ... }; }

  But that kind of defeats the major advantage of lambdas (and one of the
main reasons they were introduced): To be able to write small functions
"inline". You could perfectly well write that function without the lambda
and use it for the same purpose.

[toc] | [prev] | [next] | [standalone]


#10362

FromVictor Bazarov <v.bazarov@comcast.invalid>
Date2011-09-21 18:25 -0400
Message-ID<j5do9q$gf4$1@dont-email.me>
In reply to#10356
On 9/21/2011 3:14 AM, Juha Nieminen wrote:
> Noah Roberts<roberts.noah@gmail.com>  wrote:
>> No.  You can create them within templates though, which could serve
>> much the same purpose:
>>
>> template<  typename T>
>> function<void(T const&)>  printer() { return [](T const&  t) { ... }; }
>
>    But that kind of defeats the major advantage of lambdas (and one of the
> main reasons they were introduced): To be able to write small functions
> "inline". You could perfectly well write that function without the lambda
> and use it for the same purpose.

Jeez, you're going to pick on anything, aren't you?  Imagine a template 
a bit bigger than a one-line function.  Say, a 100-line function.  Or a 
class.  In one of the class template's member functions you can still 
write a lambda that will depend on the template argument[s] and as such 
will exist in as many variations (generated by the compiler) as there 
are template instantiations.  That's what Noah was trying to show you.

V
-- 
I do not respond to top-posted replies, please don't ask

[toc] | [prev] | [next] | [standalone]


#10672

FromChuanQi Tan <chuanqi.tan@gmail.com>
Date2011-10-04 22:04 -0700
Message-ID<19244099.1201.1317791064110.JavaMail.geo-discussion-forums@prng5>
In reply to#10305
I thank you can think like this:

lambdas was a function that compiler do it for you, so you cannot declare a function that have a "auto" type argument

void (auto pair)
{
   ...
}

I think compiler cannot work!

[toc] | [prev] | [next] | [standalone]


#11050

FromJerry <jerry.jeremiah@gmail.com>
Date2011-10-23 15:21 -0700
Message-ID<c0b071ac-c330-4aa2-89d1-707ddabd1b13@h23g2000pra.googlegroups.com>
In reply to#10672
On Oct 5, 6:04 pm, ChuanQi Tan <chuanqi....@gmail.com> wrote:
> I thank you can think like this:
>
> lambdas was a function that compiler do it for you, so you cannot declare a function that have a "auto" type argument
>
> void (auto pair)
> {
>    ...
>
> }
>
> I think compiler cannot work!

Yes, I would never have considered doing this with a free function
because its obvious that the compiler wouldn't know.  And yet in the
lambda it seemed to be such a good idea even thought it obviously the
same exact thing.

Thanks,

Jerry

[toc] | [prev] | [next] | [standalone]


#11061

FromMiles Bader <miles@gnu.org>
Date2011-10-24 17:27 +0900
Message-ID<buovcrekd59.fsf@dhlpc061.dev.necel.com>
In reply to#11050
Jerry <jerry.jeremiah@gmail.com> writes:
>> lambdas was a function that compiler do it for you, so you
>> cannot declare a function that have a "auto" type argument
>>
>> void (auto pair) { ... }
>>
>> I think compiler cannot work!
>
> Yes, I would never have considered doing this with a free
> function because its obvious that the compiler wouldn't know.
> And yet in the lambda it seemed to be such a good idea even
> thought it obviously the same exact thing.

In the case of a named function, "void fun (auto x) { ... }"
would basically be a more natural syntax for
"template<typename T> void fun (T x) { ... }"

It's discussed here:

http://channel9.msdn.com/Shows/Going+Deep/Herb-Sutter-C-Questions-and-Answers

[Summary: both were considered and came close to getting in the
standard, but didn't go in for practical reasons.]

-Miles

-- 
Erudition, n. Dust shaken out of a book into an empty skull.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web