gpt4 book ai didi

c++ - 从 std::function 获取对象上下文

转载 作者:行者123 更新时间:2023-11-28 05:52:58 27 4
gpt4 key购买 nike

我有一个指向类成员函数的std::function,例如&Foo:barstd::function 对象是否记录对象上下文,例如,Foo*?有没有办法从 std::function 对象/类中获取上下文?

目的是查明我的 unordered_multimap 函数是否已经包含来自那个context 的函数(但不一定是同一个类成员函数)。

例子;

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 的内容。

关于c++ - 从 std::function 获取对象上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34833174/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com