Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > C++11 lambdas

Reply
Thread Tools

C++11 lambdas

 
 
Jerry
Guest
Posts: n/a
 
      09-19-2011
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

 
Reply With Quote
 
 
 
 
Lucien Coffe
Guest
Posts: n/a
 
      09-20-2011
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'
 
Reply With Quote
 
 
 
 
Juha Nieminen
Guest
Posts: n/a
 
      09-20-2011
Jerry <(E-Mail Removed)> 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).
 
Reply With Quote
 
Miles Bader
Guest
Posts: n/a
 
      09-20-2011
Juha Nieminen <(E-Mail Removed)> 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...ns-and-Answers

-Miles

--
Omochiroi!
 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      09-20-2011
On Sep 19, 11:41*pm, Juha Nieminen <(E-Mail Removed)> wrote:
> Jerry <(E-Mail Removed)> 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) { ... }; }
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      09-21-2011
Noah Roberts <(E-Mail Removed)> 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.
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      09-21-2011
On 9/21/2011 3:14 AM, Juha Nieminen wrote:
> Noah Roberts<(E-Mail Removed)> 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
 
Reply With Quote
 
ChuanQi Tan
Guest
Posts: n/a
 
      10-05-2011
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!
 
Reply With Quote
 
Jerry
Guest
Posts: n/a
 
      10-23-2011
On Oct 5, 6:04*pm, ChuanQi Tan <(E-Mail Removed)> wrote:
> I thank you can think like this:
>
> lambdas was a function that compiler do it for you, so you cannot declarea 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
 
Reply With Quote
 
Miles Bader
Guest
Posts: n/a
 
      10-24-2011
Jerry <(E-Mail Removed)> 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...ns-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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C#3.0 and lambdas bearophileHUGS@lycos.com Python 51 09-26-2005 05:01 AM
lambdas vs functions: a bytecode question Fernando Perez Python 2 12-18-2004 01:28 AM
Lambdas and variables John Fouhy Python 6 08-02-2004 10:46 PM
pickling lambdas? gong Python 4 01-03-2004 11:55 PM
lambdas (lambda x: printx) Michal Python 1 12-03-2003 08:54 PM



Advertisments