我正在玩转发功能,然后我发现这个拒绝编译的特定样本(VC ++ 14)
#include <utility>
#include <functional>
template<typename Function, typename... Args>
auto test(Function&& fun, Args&&...args)
{
using retType = decltype(fun(std::forward<Args>(args)...));
return std::function<retType(decltype(std::forward<Args>(args))...)>(fun);
}
int main()
{
test([] () {});
}
这里有趣的一点是,在主要功能中我输入
int main()
{
int i = 3;
test([] (int){}, i);
}
相反,它编译得很好。
以下是整个编译器日志:
1>------ Build started: Project: Text2, Configuration: Debug Win32 ------
1> main.cpp
1>c:\users\russe\documents\visual studio 2015\projects\text2\main.cpp(9): error C2974: 'std::function': invalid template argument for '_Fty', type expected
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\functional(463): note: see declaration of 'std::function'
1> c:\users\russe\documents\visual studio 2015\projects\text2\main.cpp(15): note: see reference to function template instantiation 'auto test<main::<lambda_35d7262d4ab24dcbfd3e70d650f3eafb>,>(Function &&)' being compiled
1> with
1> [
1> Function=main::<lambda_35d7262d4ab24dcbfd3e70d650f3eafb>
1> ]
1>c:\users\russe\documents\visual studio 2015\projects\text2\main.cpp(9): error C2955: 'std::function': use of class template requires template argument list
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\functional(463): note: see declaration of 'std::function'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
你怎么看?我错过了什么吗?
感谢您愿意给我的任何帮助!
我开始思考......这可能是编译器错误吗?我的下一步是尝试在GCC下(在cpp.sh上)编译该样本,并且它可以完美地工作(http://cpp.sh/6pyq)