为什么“自动”不可接受为lambda参数 [英] Why "auto" is not acceptable as lambda parameter
本文介绍了为什么“自动”不可接受为lambda参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么此代码会导致编译错误?
Why does this code make a compile error?
std::find_if(std::begin(some_list), std::end(some_list), [](const auto& item){
//some code
});
当然在auto的错误?为什么不可能自动知道类型?
感谢
The error of course at "auto"? why is not possible to know the type automatically ? thanks
推荐答案
这是因为从C ++ 11开始,C ++中的lambda函数不能一般定义,因此您不能使用 auto
声明参数。这已经添加到C ++ 14(并且已经被一些编译器支持)。
This is because as of C++11, lambda functions in C++ cannot be defined generically, therefore you cannot declare a parameter using auto
. This has been added in the C++14 (and is already supported by some compilers).
但是,你可以在C ++ 11中使用 decltype()
,在这种情况下:
However, you can achieve the same thing in C++11 using decltype()
, in you case:
std::find_if(std::begin(some_list), std::end(some_list), [](decltype(*some_list.begin())& item){
return item > 4;
这篇关于为什么“自动”不可接受为lambda参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文