Dav*_*vid 3 c++ type-traits c++11
auto lambda = [](){ return 7; };
std::result_of<decltype(lambda)()>::type val = lambda(); // 'val' : illegal use of type 'void'
Run Code Online (Sandbox Code Playgroud)
我收到错误:'val' : illegal use of type 'void'.为什么类型会解析为无效?
我可能弄错了什么result_of让我.我只想要任何我能传递的返回值std::function.
如果您的编译器无法编译,那么不要使用std::result_of:
decltype(lambda()) val = lambda();
Run Code Online (Sandbox Code Playgroud)
这是完全一样的,它应该(好吧,可以)在VC2010中工作.
您也可以使用auto,但我不认为这是您想要的:
auto val = lambda();
Run Code Online (Sandbox Code Playgroud)
编辑:因为你在函数的返回值中使用它,所以decltype上面显示的解决方案工作正常:
#include <type_traits>
#include <iomanip>
#include <iostream>
template<class Functor>
auto foo(const Functor &f) -> decltype(f()) {
return f();
}
int main() {
auto lambda = [](){ return 7; };
auto val = foo(lambda);
std::cout << std::boolalpha;
std::cout << std::is_same<decltype(val), int>::value << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
在这里演示.
| 归档时间: |
|
| 查看次数: |
1040 次 |
| 最近记录: |