C++11 lambdas

Discussion in 'C++' started by Jerry, Sep 19, 2011.

  1. Jerry

    Jerry Guest

    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
     
    Jerry, Sep 19, 2011
    #1
    1. Advertising

  2. Jerry

    Lucien Coffe Guest

    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'
     
    Lucien Coffe, Sep 20, 2011
    #2
    1. Advertising

  3. Jerry <> 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).
     
    Juha Nieminen, Sep 20, 2011
    #3
  4. Jerry

    Miles Bader Guest

    Juha Nieminen <> 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!
     
    Miles Bader, Sep 20, 2011
    #4
  5. Jerry

    Noah Roberts Guest

    On Sep 19, 11:41 pm, Juha Nieminen <> wrote:
    > Jerry <> 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) { ... }; }
     
    Noah Roberts, Sep 20, 2011
    #5
  6. Noah Roberts <> 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.
     
    Juha Nieminen, Sep 21, 2011
    #6
  7. On 9/21/2011 3:14 AM, Juha Nieminen wrote:
    > Noah Roberts<> 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 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
     
    Victor Bazarov, Sep 21, 2011
    #7
  8. Jerry

    ChuanQi Tan Guest

    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!
     
    ChuanQi Tan, Oct 5, 2011
    #8
  9. Jerry

    Jerry Guest

    On Oct 5, 6:04 pm, ChuanQi Tan <> 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
     
    Jerry, Oct 23, 2011
    #9
  10. Jerry

    Miles Bader Guest

    Jerry <> 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.
     
    Miles Bader, Oct 24, 2011
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michal

    lambdas (lambda x: printx)

    Michal, Dec 3, 2003, in forum: Python
    Replies:
    1
    Views:
    319
    Erik Max Francis
    Dec 3, 2003
  2. gong

    pickling lambdas?

    gong, Jan 3, 2004, in forum: Python
    Replies:
    4
    Views:
    433
    John Roth
    Jan 3, 2004
  3. John Fouhy

    Lambdas and variables

    John Fouhy, Jul 28, 2004, in forum: Python
    Replies:
    6
    Views:
    386
    John Fouhy
    Aug 2, 2004
  4. Fernando Perez

    lambdas vs functions: a bytecode question

    Fernando Perez, Dec 18, 2004, in forum: Python
    Replies:
    2
    Views:
    336
    Fernando Perez
    Dec 18, 2004
  5. C#3.0 and lambdas

    , Sep 18, 2005, in forum: Python
    Replies:
    51
    Views:
    1,340
    Gregory Bond
    Sep 26, 2005
Loading...

Share This Page