#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?
question from:
https://stackoverflow.com/questions/7709894/using-auto-in-a-lambda-function 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…