use*_*893 4 c++ lambda gcc c++11
我设法将我的案例减少到以下最简单的代码:
#include <type_traits>
auto call(const auto& f) -> typename std::result_of<decltype(f)()>::type
{
return f();
}
int main()
{
return call([] { return 0; });
}
Run Code Online (Sandbox Code Playgroud)
gcc-4.9.2和gcc-5.0.0都没编译!
两人都认为"call"应该返回一个lambda函数!不知道"call"返回一个int.
这是编译器中的错误还是我的c ++关闭?非常感谢.
Pra*_*ian 13
您的代码无效C++,因为函数参数类型不可能auto,这个语法已经为Concepts Lite提出,并且可能在将来成为该语言的一部分.
result_of 需要一个调用表达式,从中推导出返回类型.
修复这两个问题,你的代码就变成了
template<typename F>
auto call(F const& f) -> typename std::result_of<decltype(f)()>::type
// or typename std::result_of<F()>::type
{
return f();
}
Run Code Online (Sandbox Code Playgroud)
或者你可以使用
template<typename F>
auto call(F const& f) -> decltype(f())
{
return f();
}
Run Code Online (Sandbox Code Playgroud)
我认为你的原始代码应该编译,如果你修复result_of表达式,但它不在gcc-4.9或5.0; 也许这是gcc扩展允许参数类型的错误auto
// This fails to compile
auto call3(const auto& f) -> typename std::result_of<decltype(f)()>::type
{
return f();
}
Run Code Online (Sandbox Code Playgroud)