比较 std::function<> 和

发布于 2024-11-04 11:44:39 字数 163 浏览 0 评论 0 原文

是否可以以某种方式比较两个 std::tr1::function<> 对象?如果我有一个 function 对象集合并想要添加和删除事件处理程序,该怎么办?添加是微不足道的,但找到要删除的似乎是不可能的。

Is it possible to somehow compare two std::tr1::function<> objects? What if I have a collection of function<void(int,float)> objects and want to add and remove event handlers? Adding is trivial, but finding the one to be removed seems to be impossible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深居我梦 2024-11-11 11:44:40

简单来说,做不到。 std::function (在其所有迭代中,包括 boost::functionstd::tr1::function)不支持 <代码>运算符==

Can't be done, put simply. std::function (in all of it's iterations, including boost::function and std::tr1::function) does not support operator==.

晨与橙与城 2024-11-11 11:44:40

根据以下链接中 Stack Overflow 的信息,这是可能的,但前提是将 std::function 对象包装在其自己的类中。

std::vector of std::function

通过使用包装类,可以测试两个包装的 std 是否:: 函数指针是相等的,但这并没有告诉您有关 std::function 包装的内容的任何信息。因此,改变你的设计可能是更好的方法。

编辑:我回来展示了我解决非常相似问题的方法。

0) Typedefs 为了简洁。

    using std::placeholders;
    typedef std::function < void ( int, float ) > some_func;
    typedef std::pair < intptr_t, intptr_t > method_hash;
  1. 通过将指针绑定到方法或函数来编写 std::function 对象的集合。当您对静态函数执行此操作时,请省略 some_object_ptr。

    some_func some_method ( std::bind ( some_method_ptr, 
                                        一些_object_ptr,_1,_2) 
    
  2. 使用 std::reinterpret_cast < intptr_t >为您的函数创建一个唯一的哈希,并将其与 std::pair 一起使用以对方法执行此操作。

     method_hashpairID (reinterpret_cast < intptr_t > ( some_object_ptr ), 
                          重新解释_cast < intptr_t > ( some_method_ptr ) );
    
  3. 现在你的pairID可以存储在向量或其他容器/数组中。只需确保保持索引对齐,以便哈希始终对应于正确的 std::function 对象,然后您可以使用 find() 获取迭代器到其位置和距离 () 将迭代器转换为必需的索引。

请注意,每次生成容器时都必须执行此操作。由于它基于指针,因此哈希值会随着程序的不同运行而变化。

Based on information from Stack Overflow at the following link, it IS possible but only if you wrap the std::function object in its own class.

std::vector of std::function

By using a wrapper class, you can test whether two wrapped std::function pointers are equal, but that doesn't tell you anything about what the std::function wraps. So, changing your design is probably a better approach.

edit: I came back to show the way I've solved a very similar problem.

0) Typedefs for conciseness.

    using std::placeholders;
    typedef std::function < void ( int, float ) > some_func;
    typedef std::pair < intptr_t, intptr_t > method_hash;
  1. Write your collection of std::function objects by binding pointers to methods or functions. Where you are doing this for static functions, omit some_object_ptr.

    some_func some_method ( std::bind ( some_method_ptr, 
                                        some_object_ptr, _1, _2 ) 
    
  2. Use std::reinterpret_cast < intptr_t > to create a unique hash for your function, and use it with std::pair to do so for methods.

     method_hash pairID ( reinterpret_cast < intptr_t > ( some_object_ptr ), 
                          reinterpret_cast < intptr_t > ( some_method_ptr ) );
    
  3. Now your pairID can be stored in a vector or other container/array. Just be sure to maintain that the indices are aligned so that a hash always corresponds to the correct std::function object, and you can then use find ( ) to get an iterator to its position and distance ( ) to convert the iterator to the required index.

Note that this will have to be done every time your container is generated. Since it's based on pointers, the hashes will change over different runs of your program.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文