【发布时间】:2015-12-12 17:40:30
【问题描述】:
我有以下课程:
class FunctionCallback
{
public:
static CallbackHandle Create(const std::function<void(void)> &function);
template<typename T,typename... TARGS>
static CallbackHandle Create(const std::function<T(TARGS...)> &function);
};
然后我像这样调用“创建”:
FunctionCallback::Create<void,float>([](float f) -> void {}); // Whether I use a lambda-function or a function pointer makes no difference
即使这应该是正确的 (?),Visual Studio 还是会在读取的消息中强调该行:
Error: no instance of overloaded function "FunctionCallback::Create" matches the argument list
argument types are: (lambda []void (float f)->void)
但是,该程序在 Visual Studio 中编译很好,没有任何警告或错误。 g++-5 无法通过类似的消息完全编译它。
当更改为:
FunctionCallback::Create<void,float>(std::function<void(float)>([](float f) -> void {}));
它不再显示消息,并且在 windows 和 linux 上都可以编译。
除非我明确指定,否则为什么不能正确推断类型?
【问题讨论】:
-
尽管 vs.net 对错误的预测是对还是错。使用模板时不要在编译前依赖它的消息。
标签: c++ templates c++11 variadic-templates implicit-conversion