Ergebnis von boost::bind in boost::function verpacken und als Template-Argument als Callback uebergeben.



  • Hi:
    Ich hab ein Problem mit boost::bind und boost::function, habs in etwa auf folgendes runterminimiert:

    #include <iostream>
    #include <boost/bind.hpp>
    #include <boost/function.hpp>
    using namespace std;
    using namespace boost;
    
    class Dummy
    {
    	public:
    	void execute_me(int d)
    	{
    		cout << "hello: " << d << '\n';
    	}
    };
    
    template<class Function>
    void run(Function f)
    {
    	f(42);
    }
    
    template<class Function>
    void wrapper(Function f)
    {
    	boost::function<void()> g = bind(&run<Function>, f);
    	g();
    }
    
    int main()
    {
    	Dummy d;
    	wrapper(bind(&Dummy::execute_me, ref(d), _1));
    	return 0;
    }
    

    Das ganze fuehrt beim GCC zu einer wahren Textwueste an Fehlermeldungen, kann mir wer sagen was falsch ist?

    EDIT: Code hatte Fehler.



  • So, hab das Problem mittlerweile selbst geloest: Wenn das Ergebnis von boost::bind explizit in einer boost::function gespeichert wird, funktioniert das Ganze:

    int main()
    {
    	Dummy d;
    	function<void(int)> x = bind(&Dummy::execute_me, ref(d), _1);
    	wrapper(x);
    	return 0;
    }
    

Anmelden zum Antworten