比较成员函数的std :: function [英] Comparing std::function for member functions

查看:157
本文介绍了比较成员函数的std :: function的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试搜寻这里的类似问题:

问题1

问题2



但是无论如何,我不能比较成员函数。这里有一个例子:

  class ClassA 
{
public:
int add ,int b)
{
return a + b;
}
};

int main()
{
ClassA a1 {};

function< int(int,int)> f1 = bind(& ClassA :: add,a1,placeholder :: _ 1,placeholder :: _ 2);
function< int(int,int)> f2 = bind(& ClassA :: add,a1,placeholder :: _ 1,placeholder :: _ 2);

cout<< boolalpha< f1 == f2< (f1.target_type()== f2.target_type())< endl // true
cout<< (f1.target
return 0;
}

从代码中可以看出 f1 f2 不同。第一个 cout 显示 true ,因为类型相同,可以。但为什么第二个 cout true ?为什么 function :: target()返回 nullptr



PS:我想创建一个简单的委托系统我可以传递任何函数(全局,静态,成员)。使用 std :: function 可以添加回调,但我不知道如何删除回调。

解决方案

这是因为 f1 的目标类型不是 int(ClassA :: *)(int,int)。它的目标类型将是 bind 表达式的结果,它在gcc上恰好是:

  std :: _ Bind< std :: _ Mem_fn< int(ClassA :: *)(int,int)> (
ClassA,
std :: _ Placeholder< 1>,
std :: _ Placeholder< 2>)>

您可以使用ABI demangler查看:

  #include< cxxabi.h> 
//注意,下面的行技术上是泄漏的,但是
//只是为了说明的目的
cout<< abi :: __ cxa_demangle(f1.target_type()。name(),0,0,0)< endl

请注意,如果目标类型实际上是一个类方法,您将无法调用两个 int s - 您还需要 ClassA * 。例如, 函数的目标类型是 int(ClassA :: *)(int,int)

  function< int(ClassA *,int,int)> f3 =& ClassA :: add; 


I tried to search and here similar questions:
question 1
question 2

But anyway, I can't compare member functions. Here's an example:

class ClassA
{
public:
    int add(int a, int b)
    {
        return a + b;
    }
};

int main()
{
    ClassA a1{};

    function<int(int, int)> f1 = bind(&ClassA::add, a1, placeholders::_1, placeholders::_2);
    function<int(int, int)> f2 = bind(&ClassA::add, a1, placeholders::_1, placeholders::_2);

    cout << boolalpha << "f1 == f2 " << (f1.target_type() == f2.target_type()) << endl; // true
    cout << (f1.target<int(ClassA::*)(int, int)>() == nullptr) << endl; // true

    return 0;
}

From the code it's obvious that f1 and f2 are different. The first cout shows true because the types are the same, it's ok. But why the second cout is true? Why function::target() returns nullptr?

P.S.: I want to create a simple delegate system so I can pass any function (global, static, member) around. With std::function I can add a callback, but I don't know how to remove it.

解决方案

That's because f1's target type is not int(ClassA::*)(int, int). Its target type is going to be the result of that bind expression, which on gcc happens to be:

std::_Bind<std::_Mem_fn<int (ClassA::*)(int, int)> (
    ClassA, 
    std::_Placeholder<1>, 
    std::_Placeholder<2>)>

Which you can see using the ABI demangler:

#include <cxxabi.h>
// note, the following line technically leaks, but 
// for illustrative purposes only it's fine
cout << abi::__cxa_demangle(f1.target_type().name(), 0, 0, 0) << endl;

Note that if the target type was actually a class method, you wouldn't be able to call it with two ints - you'd also need the ClassA*. For instance, this function's target type is int(ClassA::*)(int, int):

function<int(ClassA*, int, int)> f3 = &ClassA::add;

这篇关于比较成员函数的std :: function的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆