比较升压功能 - 函数签名? [英] Comparing boost functions - function signature?

查看:155
本文介绍了比较升压功能 - 函数签名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要开始的,我在这里阅读等等主题的其他议题,我读过的升压常见问题藏汉,我仍然不觉得我有一个答案我下面的问题。

To start of, I have read the other topics on the subject here on SO and I've read the boost faq aswell and I still do not feel I have an answer to my problem below.

我想用函数封装一样的boost ::功能有一个排序的代表制度。我通过在存储载体的boost ::函数对象解决这个问题。问题来了像UnregisterCallback()的方法,其中一个希望比较与存储在那些提供的回调函数,如果发现将其删除。从我身边的网云集,这是因为升压功能对象不是compareable。

I want to have a sort-of delegate system using function wrappers like boost::function. I solve this by storing boost::function objects in a vector. The problem comes with a method like UnregisterCallback(), where one expects to compare the supplied callback with the ones stored and remove it if found. From what I've gathered around the webs, it is because boost function objects are not compareable.

不过,使用模板按#2,我可以让它工作,我想这一点。见下面的例子:

Yet, using a template as per #2, I can make it work as I want it too. See example below:

#include <vector>
#include <algorithm>
#include "boost/function.hpp"

typedef boost::function<void (int)> Callback;

class CallbackHandler
{
public:
    void AddCallback(const Callback& callback)
    {
        mCallbacks.push_back(callback);
    }

    // #1: dosnt work
    void RemoveCallback(const Callback& callback)
    {
        mCallbacks.erase(std::find(mCallbacks.begin(), mCallbacks.end(), callback));
    }

    // #2: works
    template <typename T>
    void RemoveCallback(const T& callback)
    {
        mCallbacks.erase(std::find(mCallbacks.begin(), mCallbacks.end(), callback));
    }

private:
    std::vector<Callback> mCallbacks;

};


void testCB(int i)
{
}


int _tmain(int argc, _TCHAR* argv[])
{
    CallbackHandler handler;
    handler.AddCallback(testCB);
    handler.RemoveCallback(testCB);

    return 0;
}

但我不能/不想使用模板函数,所以我想知道,因为它显然那里工作必须是有效的函数签名,使其工作吗?然而,我不能为我的生命弄清楚它是什么,或者为什么模板版本的作品,而其他dosnt。

But I cannot/do not want to use a templated function, so I was wondering, since it obviously works there must be a valid function signature to make it work right? Yet I cannot for the life of me figure out what it is, or why the template version works while the other dosnt.

任何帮助是AP preciated。

Any help is appreciated.

感谢

推荐答案

的boost ::功能对象可以比其他仿函数和函数指针,而不是其他的的boost ::功能对象(见参考文献的这里)。当调用模板版本, T 变成了无效(*)(INT)函数指针,所以运算符== 是有效的,可以看到,类型相同,并简单地比较了的boost ::功能的底层函数指针与您传递的函数指针的对象。因为你是比较两个的boost ::功能对象非模板版本,这是无效的。

boost::function objects can be compared to other functors and function pointers, but not to other boost::function objects (see reference here). When you call the templated version, T becomes a void(*)(int) function pointer, so operator== is valid, can see that the types are the same, and simply compares the underlying function pointer in the boost::function object with the function pointer you passed in. For the non-templated version you are comparing two boost::function objects, which is not valid.

这篇关于比较升压功能 - 函数签名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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