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

后端 未结 5 2023
感动是毒
感动是毒 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:03

    The following (your comment to my other answer above) works:

    #include 
    #include 
    
    struct foo
    {
       template
       void operator()(T& t)
       {
          ++t;
       }
    };
    
    int main()
    {
    
       std::vector v;
       std::for_each(v.begin (),v.end(),foo());
    
       return 0;
    }
    

    But the following does not:

    #include 
    #include 
    
    template
    struct foo
    {
       void operator()(T& t)
       {
          ++t;
       }
    };
    
    int main()
    {
    
       std::vector v;
       std::for_each(v.begin (),v.end(),foo()); // <-- the syntax for foo here 
                                                //     is kinda fictitious
    
       return 0;
    }
    

    Probably the C++ committee saw lambdas as being more similar to the second example than the first. (Though I haven't figured out clever way to define a lambda in which this would make a difference. Anyone got any crazy ideas?)

提交回复
热议问题