是否可以使用 boost::bind 来有效地连接函数?

发布于 2024-08-30 19:16:12 字数 1028 浏览 7 评论 0原文

假设我有一个 boost::function ,具有名为 CallbackType 类型的任意签名。

  • 是否可以使用 boost::bind 来编写一个函数,该函数采用与 CallbackType 相同的参数,但连续调用两个函子?

我对任何潜在的可能性持开放态度解决方案,但这里有一个...

...使用一些magic模板的假设示例:

Template<typename CallbackType>
class MyClass
{
    public:
        CallbackType doBoth;

        MyClass( CallbackType callback )
        {
            doBoth = bind( magic<CallbackType>, 
                             protect( bind(&MyClass::alert, this) ),   
                               protect( callback )                    );
        }

        void alert()
        {
            cout << "It has been called\n";
        }
};

void doIt( int a, int b, int c)
{
    cout << "Doing it!" << a << b << c << "\n";
}

int main()
{
    typedef boost::function<void (int, int, int)> CallbackType;

    MyClass<CallbackType> object( boost::bind(doIt) );

    object.doBoth();

    return 0;
}

Assume that I have a boost::function of with an arbitrary signature called type CallbackType.

  • Is it possible to use boost::bind to compose a function that takes the same arguments as the CallbackType but calls the two functors in succession?

I'm open to any potential solution, but here's a...

...Hypothetical example using some magic template:

Template<typename CallbackType>
class MyClass
{
    public:
        CallbackType doBoth;

        MyClass( CallbackType callback )
        {
            doBoth = bind( magic<CallbackType>, 
                             protect( bind(&MyClass::alert, this) ),   
                               protect( callback )                    );
        }

        void alert()
        {
            cout << "It has been called\n";
        }
};

void doIt( int a, int b, int c)
{
    cout << "Doing it!" << a << b << c << "\n";
}

int main()
{
    typedef boost::function<void (int, int, int)> CallbackType;

    MyClass<CallbackType> object( boost::bind(doIt) );

    object.doBoth();

    return 0;
}

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

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

发布评论

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

评论(2

但可醉心 2024-09-06 19:16:12

Boost 已经提供了一种创建一系列绑定函数的方法。使用 Lambda 的逗号运算符

using namespace boost::lambda;
MyClass mc;
CallbackType object = (bind(&MyClass::alert, mc), bind(doIt, _1, _2, _3));
object(1, 2, 3);

这将创建一个新的函子,object。当您使用三个参数调用该函子时,它将依次调用 mc.alert(),然后将这些参数传递给 doIt。括号很重要。

为了使上面的示例正常工作,您需要将 alert 设为 const 函数。如果它需要是非常量,则可以将指针传递给mc,或者用boost::ref(mc)。并且您需要使用 Boost.Lambda 的 bind 而不是 Boost.Bind 的;后者与 Lambda 的函数组合运算符(尤其是逗号)不兼容。

Boost already provides a way to create a sequence of bound functions. Use Lambda's comma operator.

using namespace boost::lambda;
MyClass mc;
CallbackType object = (bind(&MyClass::alert, mc), bind(doIt, _1, _2, _3));
object(1, 2, 3);

That will create a new functor, object. When you invoke that functor with three arguments, it will in turn call mc.alert() before passing those arguments to doIt. The parentheses are important.

For my example above to work, you'd need alert to be a const function. If it needs to be non-const, then either pass a pointer to mc, or wrap it with boost::ref(mc). And you'll need to use Boost.Lambda's bind rather than Boost.Bind's; the latter isn't compatible with Lambda's function-combining operators (comma, in particular).

后来的我们 2024-09-06 19:16:12
template< class Callback >
struct pre_caller {
    Callback c;

    pre_caller( Callback in_c ) : c( in_c ) {}

    void alert() {} // or an instance of a functor

    operator()
    { alert(); c(); }

    template< class T1 >
    operator( T1 a ) // not sure if/what qualification to add to a
    { alert(); c( a ); } // or whether to attempt to obtain from
                         // function_traits<Callback>?
    template< class T1, class T2 >
    operator( T1 a, T2 b )
    { alert(); c( a, b ); }

    template< class T1, class T2, class T3 >
    operator( T1 a, T2 b, T3 c )
    { alert(); c( a, b, c ); }

    // ad nauseam... and I mean nausea, maybe read up on Boost Preprocessor.
};

Boost Bind 在其可变参数中使用了大量的预处理器黑客技术,不幸的是,我认为它没有提供用于头部修补的模式或工具,而这本质上就是这样。

template< class Callback >
struct pre_caller {
    Callback c;

    pre_caller( Callback in_c ) : c( in_c ) {}

    void alert() {} // or an instance of a functor

    operator()
    { alert(); c(); }

    template< class T1 >
    operator( T1 a ) // not sure if/what qualification to add to a
    { alert(); c( a ); } // or whether to attempt to obtain from
                         // function_traits<Callback>?
    template< class T1, class T2 >
    operator( T1 a, T2 b )
    { alert(); c( a, b ); }

    template< class T1, class T2, class T3 >
    operator( T1 a, T2 b, T3 c )
    { alert(); c( a, b, c ); }

    // ad nauseam... and I mean nausea, maybe read up on Boost Preprocessor.
};

Boost Bind uses a lot of preprocessor hacking for its variadic voodoo, and unfortunately I don't think it provides a pattern or tools for head-patching which is essentially what this is.

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