Is it possible to use boost::bind to effectively concatenate functions?

Posted by Catskul on Stack Overflow See other posts from Stack Overflow or by Catskul
Published on 2010-04-29T21:14:25Z Indexed on 2010/04/29 21:17 UTC
Read the original article Hit count: 344

Filed under:
|
|
|
|

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?

Hypothetical example using a 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;
}

© Stack Overflow or respective owner

Related posts about c++

Related posts about boost