#include <vector>
#include <algorithm>
void foo( int )
{
}
int main()
{
std::vector< int > v( { 1,2,3 } );
std::for_each( v.begin(), v.end(), []( auto it ) { foo( it+5 ); } );
}
When compiled, the example above starts the error output like this :
h4.cpp: In function 'int main()':
h4.cpp:13:47: error: parameter declared 'auto'
h4.cpp: In lambda function:
h4.cpp:13:59: error: 'it' was not declared in this scope
Does it mean that the keyword auto
should not be used in lambda expressions?
This works :
std::for_each( v.begin(), v.end(), []( int it ) { foo( it+5 ); } );
Why the version with the auto keyword doesn't work?
See Question&Answers more detail:os