我得到以下错误,试图声明指向模板类成员的std::函数。
错误C2672 '
std::invoke':没有找到匹配的重载函数
template <typename InputArchive, typename OutputArchive, typename ... Components>
class LevelSerializer {
...
template <typename Component>
void initRegistry() {
...
std::function<void(entityx::Entity, OutputArchive)> f(std::bind(&LevelSerializer<InputArchive, OutputArchive, Components...>::saveComponent<Component>, this)); // errors
}
...
// I'm trying to point to an instance of this function
template <typename T>
void saveComponent(entityx::Entity& entity, OutputArchive& archive)
};entityx::Entity是一个固定的(非模板)类型。为什么这样做失败了?
发布于 2015-12-23 08:44:50
你有两个问题:第一,你的台词:
std::function<void(entityx::Entity, OutputArchive)>
f(std::bind(&LevelSerializer<InputArchive, OutputArchive, Components...>::saveComponent<Component>, this)); // errors应该是
typedef std::function<void(entityx::Entity, OutputArchive) functype;
typedef LevelSerializer<InputArchive, OutputArchive, Components...> LS;
functype f(std::bind(&LS::saveComponent<Component>, this,
std::placeholders::_1, std::placeholders::_2 ));您的问题是,正如您已经编写了它,您对std::bind的调用是试图返回一个没有参数的函数(并且它最终没有足够的参数用于您试图调用的成员函数)。您需要绑定place参数,以便a)成员函数有足够的参数;b)结果是有两个参数的东西。
旁白:在LevelSerializer模板中,裸LevelSerializer引用带有参数的模板。所以实际上,你只需要:
typedef std::function<void(entityx::Entity, OutputArchive) functype;
functype f(std::bind(&LevelSerializer::saveComponent<Component>, this,
std::placeholders::_1, std::placeholders::_2 ));第二个问题是签名不匹配(这要感谢Piotr )。function的模板参数用于一个按值接受两个参数的函数。您的成员函数通过非const引用接受两个参数。您至少需要将模板参数更改为:
typedef std::function<void(entityx::Entity&, OutputArchive&) functype;
functype f(std::bind(&LevelSerializer::saveComponent<Component>, this,
std::placeholders::_1, std::placeholders::_2 ));..。但是,您可能希望将entity参数更改为const。
发布于 2016-04-30 22:14:02
Martin Bonner更直接地回答了这个问题,但我想我应该注意到,在我的例子中,我收到了以下代码的错误:
std::bind(&ClassName::FuncName, objName, _1);并按如下方式修正:
std::bind(&ClassName::FuncName, objName, std::placeholders::_1);显然,我的一个标头包含了一个带有占位符的boost库,_1指向的是boost版本而不是我的版本。
https://stackoverflow.com/questions/34431582
复制相似问题