6

Is it safe to use std::bind to pass a member function to boost::signals2::signal::connect()? In other words, is boost::bind and std::bind interchangeable?

It compiles with VC++ 2010 SP1, but the template code is way over my head and I'm afraid I might be venturing into undefined behaviour territory.

links77
  • 1,534
  • 4
  • 14
  • 20

2 Answers2

2

I'm not experienced in the subject by I would expect connect to take anything that implements a valid function call operator. It should be safe to call it with any function or function object that matches the signature, be it boost::bind, std::bind or anything else. Boost libraries are designed to be generic, so they usually don't go picking each others implementation details.

K-ballo
  • 80,396
  • 20
  • 159
  • 169
1

The connect function takes a boost::function object, which is basically a generic wrapper around anything that has an operator() defined for it. Therefore it is exactly as safe as what you are binding.

For example, this is reasonably safe:

boost::shared_ptr<ClassName> pValue = boost::make_shared<ClassName>(...);
signal.connect(boost::bind(&ClassName::FuncName, pValue, ...);

This is reasonably safe because it stores a boost::shared_ptr as part of its data.

ClassName *pValue = new ClassName(...);
signal.connect(boost::bind(&ClassName::FuncName, pValue, ...);

This is conditionally safe. It instantly becomes unsafe if that connection still exists and you execute delete pValue.

Personally, I don't put much faith in "conditionally safe", but that's up to you. The point being that everything you bind to boost::bind must continue to exist so long as it is bound.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982