简体   繁体   中英

Is it safe to use std::bind with boost::signals2?

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM