从 std::function 获取对象上下文
Get object context from std::function
我有一个指向类成员函数的std::function
,例如&Foo:bar
。std::function
对象是否记录对象上下文,例如Foo*
?有没有办法从std::function
对象/类中获取上下文?
目的是找出我的函数unordered_multimap
是否已经包含该上下文中的函数(但不一定是同一个类成员函数)。
例;
std::unordered_multimap<int, std::function<int(int)>> callbacks;
Foo* myFoo = new Foo();
callbacks[1].emplace( std::bind(&Foo::bar, myFoo, std::placeholders::_1) );
callbacks[1].emplace( std::bind(&Foo::bar2, myFoo, std::placeholders::_1) ); // now callbacks contains 2 functions for the same object/context. I want to avoid this
// Check context exists before inserting
auto vCallbacks = callbacks.equal_range(1);
for (auto iter = vCallbacks.first; iter != vCallbacks.second; iter++) {
std::function<int(int)> func = iter->second;
// somehow check function object for context myFoo
if (func.??? == myFoo)
// Sorry cant add callback because this object already has registered one
}
不,它没有。您基本上无法以任何有用的方式检查std::function
的内容。
相关文章:
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- CMake-按正确顺序将项目与C运行时对象文件链接
- 我的一些 D3D11 对象在调用某些设备上下文函数时将自身作为 ComPtrs 为空
- 如何将图像的字节数组(取自 s3)转换为 C++ 或 C 中的图像/图像对象
- 当删除上下文属性中的QLIST对象时,QT QML应用程序崩溃
- 线程上下文上的静态存储对象优化
- C++:如何创建一个临时对象,包含一个指针 - 常量或非常量,具体取决于上下文
- C++ V8 对象上下文
- 在未加载上下文的情况下在对象构造函数中使用OpenGL函数
- CORBA omniORB在解析名称上下文后无法获取远程对象
- 使用设备上下文在CImage对象上绘制线
- Boost.python 将对象拉取到本地范围内以进行读取访问
- 从 std::function 获取对象上下文
- 取两个size_t对象的差异是否安全
- C++类方法,可以使用虚拟调度或类似于没有对象上下文的静态函数来调用
- 从gdiplus (GDI+)中的设备上下文或图形对象中获取图像/位图
- 如何使用英特尔预取pragma时,数据隐藏在一个对象
- (Windows)何时删除对象和设备上下文
- QML 报告引用错误:XYZ 未在添加到上下文C++对象上定义