
Hello, I am trying to use Boost::Bind to bind my function but I am getting an error as Bind cannot diffrentiate between the constructors even though they have diffrent function signatures. TestBind(boost::bind((&Test::process), &t1, _1)); I would highly appreciate any help. Thanks //Boost Includes #include <boost/function.hpp> #include <boost/bind.hpp> #include <boost/enable_shared_from_this.hpp> namespace test{ struct A{ public: int one; int two; }; struct B{ public: short three; short four; }; typedef boost::shared_ptr<A> t_boostInt; typedef boost::shared_ptr<B> t_boostShort; class TestBind{ public: typedef boost::function< int (t_boostInt)> t_ProcessFunctionInt; public: ///< call back process function for short typedef boost::function< short (t_boostShort)> t_ProcessFunctionShort; public: TestBind( t_ProcessFunctionInt func){} TestBind(t_ProcessFunctionShort func){} }; class Test { public: Test(){} ~Test(); public: int process(t_boostInt msg) { return 1;} }; int main(int argc, char * argv[]) { try { Test t1; TestBind(boost::bind((&Test::process), &t1, _1)); } catch (std::exception & exception) { std::cout << exception.what() << std::endl; } return 0; } }

Hi Vishal, vishal rawate wrote:
Hello,
I am trying to use Boost::Bind to bind my function but I am getting an error as Bind cannot diffrentiate between the constructors even though they have diffrent function signatures.
TestBind(boost::bind((&Test::process), &t1, _1));
I would highly appreciate any help.
Thanks
This is a problem with binding overloaded functions. See <http://www.boost.org/doc/libs/1_54_0/libs/bind/bind.html#err_overloaded> I typically static_cast, when I can't use a C++11 lambda. HTH, Nate

I am trying to use Boost::Bind to bind my function but I am getting an error as Bind cannot diffrentiate between the constructors even though they have diffrent function signatures.
TestBind(boost::bind((&Test::process), &t1, _1));
This is a problem with binding overloaded functions. See <http://www.boost.org/doc/libs/1_54_0/libs/bind/bind.html#err_overloaded> I typically static_cast, when I can't use a C++11 lambda.
Nate, as far as I can see this isn't an issue with binding to an overloaded function (because the function isn't overloaded!), it's selecting the correct constructor overload based on the return value of boost::bind. Putting a temporary TestBind::t_ProcessFunctionInt in between the creation of the TestBind object and the bind to Test::process works; it's the implicit conversion between the return value of boost::bind and the two constructor overloads which is failing. ie: this works: TestBind::t_ProcessFunctionInt fn = boost::bind((&Test::process), &t1, _1); TestBind f(fn); this doesn't: TestBind g(boost::bind((&Test::process), &t1, _1)); In other words, it's the choice between TestBind(t_ProcessFunctionInt) {} TestBind(t_ProcessFunctionShort) {} which fails Note that making the constructors explicit doesn't work: explicit TestBind(t_ProcessFunctionInt) {} explicit TestBind(t_ProcessFunctionShort) {}

Hi Steve, Steve Lorimer wrote:
Nate, as far as I can see this isn't an issue with binding to an overloaded function (because the function isn't overloaded!), it's selecting the correct constructor overload based on the return value of boost::bind.
You're absolutely right. Sorry for the bad advice. I agree with your solution. Thanks, Nate

I'm puzzled as to why making the constructors explicit doesn't work. I've posted a question on stackoverflow using std::bind / std::function (but the gist is the same) http://stackoverflow.com/questions/18939107/return-type-of-stdbind-implicitl...
participants (3)
-
Nathan Crookston
-
Steve Lorimer
-
vishal rawate