2
votes
#include <functional>
#include <boost/bind.hpp>
class A
{
public:
    A(){}
    ~A(){}
    template<typename _Handler>
    void call_handler(_Handler handler)
    {
        handler();
    }
};

class B
{
public:
    template<typename _Handler>
    void call_handler(_Handler handler)
    {

    }
    template<typename _Handler>
    void run(_Handler handler)
    {
        m_a.call_handler(boost::bind(&B::call_handler<_Handler>, this, handler));
        //only can use boost::bind here
    }
    A m_a;
};
class Test
{
public:
    void handler()
    {

    }
};

int main()
{
    B b;
    Test t;
    b.run(boost::bind(&Test::handler,&t));//only can use std::bind here
}

this is my little test code above.

I am confused that I only can use bind in the specific order...see note above

if I change std::bind to boost::bind ,then compiler failed,vice versa.

tested with:

gcc 4.9.2 for cygwin with option:

g++ -std=c++11 -fdiagnostics-color=always -fdiagnostics-show-location=every-line -I"/cygdrive/e/opensource libs/boost" main.cpp

msvc 12.0(visual studio 2013 with update4) with default option.

gcc diagnostic messages:

In file included from /cygdrive/e/opensource libs/boost/boost/bind.hpp:22:0, from main.cpp:3: /cygdrive/e/opensource libs/boost/boost/bind/bind.hpp: In instantiation of ‘void boost::_bi::list2::operator()(boost::_bi::type, F&, A&, int) [with F = boost::_mfi::mf1, boost::_bi::list1 > > >; A = boost::_bi::list0; A1 = boost::_bi::value; A2 = boost::_bi::bind_t, boost::_bi::list1 > >]’: /cygdrive/e/opensource libs/boost/boost/bind/bind.hpp:893:50:
required from ‘boost::_bi::bind_t::result_type boost::_bi::bind_t::operator()() [with R = void; F = boost::_mfi::mf1, boost::_bi::list1 > > >; L = boost::_bi::list2, boost::_bi::bind_t, boost::_bi::list1 > > >; boost::_bi::bind_t::result_type = void]’ main.cpp:12:11:
required from ‘void A::call_handler(_Handler) [with _Handler = boost::_bi::bind_t, boost::_bi::list1 > > >, boost::_bi::list2, boost::_bi::bind_t, boost::_bi::list1 > > > >]’ main.cpp:27:3:
required from ‘void B::run(_Handler) [with _Handler = boost::_bi::bind_t, boost::_bi::list1 > >]’ main.cpp:44:38:
required from here /cygdrive/e/opensource libs/boost/boost/bind/bind.hpp:313:34: error: invalid use of void expression unwrapper::unwrap(f, 0)(a[base_type::a1_], a[base_type::a2_]); ^

msvc 12.0 diagnostic messages:

1>------ Build started: Project: scince_x32, Configuration: Debug Win32 ------ 1> main.cpp 1>e:\opensource libs\boost\boost\bind\bind.hpp(313): error C2664: 'void boost::_mfi::mf1::operator ()(T *,A1) const' : cannot convert argument 2 from 'void' to 'boost::_bi::bind_t,boost::_bi::list1>>' 1> with 1> [ 1>
_Handler=boost::_bi::bind_t,boost::_bi::list1>> 1> , T=B 1> , A1=boost::_bi::bind_t,boost::_bi::list1>> 1> ] 1> and 1> [ 1> T=Test * 1> ] 1> Expressions of type void cannot be converted to other types 1> e:\opensource libs\boost\boost\bind\bind.hpp(893) : see reference to function template instantiation 'void boost::_bi::list2,boost::_bi::bind_t,boost::_bi::list1>>>::operator ()(boost::_bi::type,F &,A &,int)' being compiled 1> with 1> [ 1> T=B * 1> ,
F=boost::_mfi::mf1,boost::_bi::list1>>> 1> , A=boost::_bi::list0 1> ] 1> e:\opensource libs\boost\boost\bind\bind.hpp(893) : see reference to function template instantiation 'void boost::_bi::list2,boost::_bi::bind_t,boost::_bi::list1>>>::operator ()(boost::_bi::type,F &,A &,int)' being compiled 1> with 1> [ 1> T=B * 1> ,
F=boost::_mfi::mf1,boost::_bi::list1>>> 1> , A=boost::_bi::list0 1> ] 1> e:\opensource libs\boost\boost\bind\bind.hpp(891) : while compiling class template member function 'void boost::_bi::bind_t,boost::_bi::list2,boost::_bi::bind_t,boost::_bi::list1>>>>::operator ()(void)' 1> with 1> [ 1>
_Handler=boost::_bi::bind_t,boost::_bi::list1>> 1> , T=B * 1> ] 1> e:\c++program\scince_x32\scince_x32\main.cpp(12) : see reference to function template instantiation 'void boost::_bi::bind_t,boost::_bi::list2,boost::_bi::bind_t,boost::_bi::list1>>>>::operator ()(void)' being compiled 1> with 1> [ 1>
_Handler=boost::_bi::bind_t,boost::_bi::list1>> 1> , T=B * 1> ] 1> e:\c++program\scince_x32\scince_x32\main.cpp(27) : see reference to class template instantiation 'boost::_bi::bind_t,boost::_bi::list2,boost::_bi::bind_t,boost::_bi::list1>>>>' being compiled 1> with 1> [ 1>
_Handler=boost::_bi::bind_t,boost::_bi::list1>> 1> , T=B * 1> ] 1> e:\c++program\scince_x32\scince_x32\main.cpp(44) : see reference to function template instantiation 'void B::run,boost::_bi::list1>>>(_Handler)' being compiled 1> with 1> [ 1> T=Test * 1> ,
_Handler=boost::_bi::bind_t,boost::_bi::list1>> 1> ] ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

1

1 Answers

2
votes

You're (indirectly) binding a bind-expression that binds as an argument another bind-expression.

In both Boost and standard library you need to protect the inner bind-expression so that the placeholders/bindings don't mix and conflict.

So it would begin with

m_a.call_handler(boost::bind(&B::call_handler<_Handler>, this, boost::protect(handler)));

Except, the protect(handler) wraps the type and it's no longer _Handler, so you need something else for &B::call_handler<???>. In my experience, the easiest by far, is to use a polymorphic function object:

struct handler_caller_f {
    typedef void result_type;

    template <typename H> void operator()(B* /*this_*/, H/* handler*/) const {
        std::cout << __PRETTY_FUNCTION__ << "\n"; 
    }
};

template <typename H> void run(H handler) {
    m_a.call_handler(boost::bind(handler_caller_f(), this, boost::protect(handler)););
}

As you can see the function object replaces B::call_handler and removes the problem by deducing the type of handler again.

Here's a cleaned up version:

Using Boost

Live On Coliru

#include <boost/bind.hpp>
#include <boost/bind/protect.hpp>
#include <iostream>

struct A {
    template <typename H> void call_handler(H handler) { 
        handler(); 
    }
};

struct B {
    struct handler_caller_f {
        typedef void result_type;

        template <typename H> void operator()(B* this_, H handler) const {
            handler();
        }
    };

    template <typename H> void run(H handler) {
        m_a.call_handler(boost::bind(handler_caller_f(), this, boost::protect(handler)));
    }

    A m_a;
};

struct Test {
    void handler() { std::cout << __PRETTY_FUNCTION__ << "\n"; }
};

int main() {
    Test t;

    B b;
    b.run(boost::bind(&Test::handler, &t));
}

Prints

void Test::handler()

Using std::bind

Standard library doesn't have protect, but it's easy to add:

Live On Coliru

#include <functional>
#include <iostream>

namespace std_ex { // http://stackoverflow.com/questions/18519087/why-is-there-no-stdprotect
template <typename T> struct protect_wrapper : T {
    protect_wrapper(const T &t) : T(t) {}
    protect_wrapper(T &&t) : T(std::move(t)) {}
};

template <typename T>
typename std::enable_if<!std::is_bind_expression<typename std::decay<T>::type>::value, T && >::type protect(T &&t) {
    return std::forward<T>(t);
}

template <typename T>
typename std::enable_if<std::is_bind_expression<typename std::decay<T>::type>::value,
                        protect_wrapper<typename std::decay<T>::type> >::type
protect(T &&t) {
    return protect_wrapper<typename std::decay<T>::type>(std::forward<T>(t));
}
}

struct A {
    template <typename H> void call_handler(H handler) { handler(); }
};

struct B {
    struct handler_caller_f {
        typedef void result_type;

        template <typename H> void operator()(B *this_, H handler) const { handler(); }
    };

    template <typename H> void run(H handler) {
        m_a.call_handler(std::bind(handler_caller_f(), this, std_ex::protect(handler)));
    }

    A m_a;
};

struct Test {
    void handler() { std::cout << __PRETTY_FUNCTION__ << "\n"; }
};

int main() {
    Test t;

    B b;
    b.run(std::bind(&Test::handler, &t));
}