Using auto in a lambda function

后端 未结 4 1599
野性不改
野性不改 2020-12-13 17:22
#include 
#include 

void foo( int )
{
}

int main()
{
  std::vector< int > v( { 1,2,3 } );

  std::for_each( v.begin(), v.end()         


        
4条回答
  •  佛祖请我去吃肉
    2020-12-13 18:19

    auto keyword does not work as a type for function arguments, in C++11. If you don't want to use the actual type in lambda functions, then you could use the code below.

     for_each(begin(v), end(v), [](decltype(*begin(v)) it ){
           foo( it + 5);         
     });
    

    The code in the question works just fine in C++ 14.

提交回复
热议问题