Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mixing boost smart pointers and C++11 smart pointers in boost::signals2

Tags:

c++

c++11

boost

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?

like image 883
OMGtechy Avatar asked Mar 16 '14 23:03

OMGtechy


People also ask

What are smart pointers in C++11?

C++11 introduced smart pointers as part of the standard library, inside the <memory> header file. Smart pointers reduce some of the manual programming work associated with memory management. They provide automated ways of detecting when to delete pointers, and try to protect developers from introducing memory leaks.

What is the template parameter of a smart pointer?

These smart pointer class templates have a template parameter, T, which specifies the type of the object pointed to by the smart pointer. The behavior of the smart pointer templates is undefined if the destructor or operator delete for objects of type T throw exceptions. T may be an incomplete type at the point of smart pointer declaration.

What is the difference between boost:: shared_ptr and boost::intrusive_PTR?

However, while boost::shared_ptr keeps track of the number of shared pointers referencing a particular object, the developer has to do this when using boost::intrusive_ptr. This can make sense if other classes already keep track of references. boost::intrusive_ptr is defined in boost/intrusive_ptr.hpp.

When is a smart pointer passed to the containers?

When a Boost.Intrusivehook is configured with a smart pointer as an argument, this pointer configuration is passed to the containers. For example, if the following hook is configured with a smart pointer (for example, an offset pointer from Boost.Interprocess):


1 Answers

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.

like image 152
OMGtechy Avatar answered Sep 29 '22 12:09

OMGtechy