C++11 and the lack of polymorphic lambdas - why?

后端 未结 5 2016
感动是毒
感动是毒 2020-12-08 00:38

I\'ve been reviewing the draft version of the C++11 standard. Specifically the section on lambdas, and I am confused as to the reasoning for not introducing polymorphic lamb

5条回答
  •  误落风尘
    2020-12-08 01:28

    Since the argument, c, meets the STL requirements for a container, you should be able to use something like

    template
    void foo(Container c)
    {
        for_each(c.begin(), c.end(),[](typename Container::reference t) { ++t; });
    }
    

    I'll also showcase John Purdy's comment above, which is another way to get the typename you want in this lambda:

    template
    void foo(Container c)
    {
       for_each(c.begin(),c.end(),[](decltype(*c.begin()) t) { ++t; });
    }
    

    (Yes, Dominar, I know you don't like this answer, because it doesn't answer your question, but I'm willing to bet that the next person who comes along asking this question is going to be looking for a way to make their code work, so it does make sense to have some techniques around where the question is relevant.)

提交回复
热议问题