定义指向函数模板实例的std::函数指针时出错,该实例是模板化类的成员
error defining std::function pointer to an instance of a function template, which is a member of a templated class?
我在试图声明一个指向模板化类成员的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
是一种固定(非模板)类型。为什么这样做会失败?
您有两个问题:首先,您的线路:
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的调用试图返回一个没有参数的函数(最终它没有足够的参数用于您试图调用的成员函数)。您需要绑定占位符参数,以便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 Skotnicki)。function
的模板参数用于一个函数,该函数按值接受两个参数。您的成员函数通过非常量引用接受两个参数。您至少需要将模板参数更改为:
typedef std::function<void(entityx::Entity&, OutputArchive&) functype;
functype f(std::bind(&LevelSerializer::saveComponent<Component>, this,
std::placeholders::_1, std::placeholders::_2 ));
但您可能希望将entity
参数更改为常量引用。
Martin Bonner更直接地回答了这个问题,但我想我应该注意到,在我的情况下,我收到了以下代码的错误:
std::bind(&ClassName::FuncName, objName, _1);
并这样修复:
std::bind(&ClassName::FuncName, objName, std::placeholders::_1);
显然,我的一个标题包含一个带有占位符的boost库,而_1指向的是boost版本,而不是我的版本。
相关文章:
- 依赖于类成员属性的类实例成员
- 实例成员与静态成员与非类方法的开销
- 将成员函数分配给实例成员的属性
- 向量映射作为实例成员的C 地图
- 如何转发声明模板实例成员函数
- 如何确保父实例成员的销毁
- 访问模板类型实例成员
- boost :: Spirit :: Qi-与语法结构中的实例成员一起工作
- 如何使用类实例成员函数指针填充函数指针成员
- C func 到C++具有非双射参数类型映射的实例成员蹦床
- 当拥有的对象退出范围时,在类实例成员上使用shared_ptr是否会正确清理
- 类实例成员初始化
- 无效SetSomeValue(int i);实例成员函数跟踪谁使用SetSomeValue()设置了SomeValue成
- 重新分配作为实例成员的数组
- 回调需要const函数,如何传递对象的实例成员
- 如何通过std::shared_ptr访问实例成员变量
- c++类实例成员是如何在机器级别传递的
- 指向实例成员的函数指针
- 如何使用 Python C/C++ 接口将实例成员函数作为 PyCFunction 类型传递
- 如何将实例成员函数作为回调传递给 std::thread