Mixing boost smart pointers and C++11 smart pointe

2019-06-17 23:03发布

问题:

I have an application which uses boost::signals2 to communicate between components. I am trying to use it's automatic connection management features through slot_type(...).track(weak_ptr).

The problem:

Throughout my program, std::shared_ptr is used. .track expects a boost::weak_ptr, and I am providing an std::weak_ptr.

Here's the exact error I'm getting:

cannot convert argument 1 from 'std::weak_ptr<_Ty>' to 'const boost::weak_ptr<void> &'

Is there a workaround for this? Or have I misunderstood the problem?

回答1:

I found a solution, and it was to use .track_foreign instead of .track. It allows the use of C++11 smart pointers in place of the boost smart pointers.



回答2:

To the eyes of C++, and the compiler, std::weak_ptr and boost::weak_ptr are two completely different classes that has nothing in common. Therefore, when you are using boost::signals2 I'd suggest you to stick with boost::weak_ptr.



标签: c++ c++11 boost