【发布时间】:2016-02-10 19:31:45
【问题描述】:
编译以下代码时:
#include <functional>
template <typename functionSignature>
class Class
{
std::function<functionSignature> func;
public:
Class(const std::function<functionSignature>& arg) : func(arg) {}
void callFunc() { func(); }
};
void f(const int i) {}
int main()
{
Class<void(const int)> a(std::bind(f, 10));
a.callFunc();
return 0;
}
VS 2015 编译器在第六行生成如下错误信息:
error C2064: term does not evaluate to a function taking 0 arguments.
现在,我相信这是因为编译器认为functionSignature 不是函数签名;例如,当我实例化并尝试在std::function<int> 而不是std::function<int()> 上调用operator() 时,也会发生同样的错误。
如何保证模板参数始终是函数签名,以便我可以在std::function 上调用operator()?
【问题讨论】:
-
您能否准确地更新您的代码,您如何调用您的课程以及您如何初始化您的课程?
-
是的,实际的 MCVE 会有所帮助。只需添加一个玩具
main,它会创建一个Class<some stuff here>的实例,然后在其上执行.callFunc(),并确认它编译并在MSVC 中生成错误。
标签: c++ function class templates c++11