Asio: Using the auto-shared pointer in the examples

Looking through the examples of the Asio library, this one for example (for instance, line 37), I see that sometimes they create a shared pointer from this (they name it self), and capture it in a lambda where they call some asio functions, but I don

Smart pointer to lambda

I'm trying to make a function that accepts a shared pointer to some functor. With manually crafted functors there're no problems, but with lambda there are. I know that I can't use decltype with lambda - every new lambda declaration creates a new typ

C ++ serial_ptr serialization

I have a class named A which I want to serialize it's object in another class named B. but I keep getting this error: error: 'class std::shared_ptr<A>' has no member named 'serialize' class A is: class A { public: typedef shared_ptr<A> Ptr; st

How to pass deleter to make_shared?

Since C++11, because of several reasons, developers tend to use smart pointer classes for dynamic lifetime objects. And with those new smart pointer classes, standards, even suggest to not use operators like new instead they suggest to use make_share

Pass a shared_ptr through a C interface that takes the void *

I have a C++ project that uses SDL, in particular SDL Events. I would like to use the event system for incoming network messages just as it is used for UI events. I can define a new event type and attach some arbitrary data (see this example). This i

Link a callback to an expiring shared_ptr?

I am familiar with std::shared_ptr and std::weak_ptr and know how they work. However, I would like the std::shared_ptr to emit a callback, like a boost signal. This would allow std::weak_ptr, who still refer to the deleted object, to be cleaned up ri

Move the constructor and = operator for the shared pointer

Say, I have a class: class GameObject ///header file { .... std::shared_ptr<Transform> transform; } ///cpp file //Copy Ctor GameObject::GameObject(const GameObject& rhs) :transform(rhs.transform) {} //Move CTor GameObject::GameObject(GameObject&

enable_shared_from_this. Why the crash?

Could someone explain why the following crashes? I'm using enable_shared_from_this so that bob does not get deleted. class Person : public std::enable_shared_from_this<Person> { private: std::string name; public: Person (const std::string& name)

When is the object pointed to by std :: shared_ptr deleted?

In my library, I use std:shared_ptr`s to hold communication objects, I am working with. I have template function creating those pointers. It returns raw pointers so application could use those objects, without overhead with reference counting (strict

Pure virtual and std :: shared_ptr

Possibly I just missed something from the documentation (or just can't do a proper Google serach), but I have issues with a shared_ptr and pure virtual functions. So a short example which works: class Base { public: virtual int stuff() = 0; }; class

Shared pointers and raw pointers in the same container

I need to populate container with shared pointers and raw pointers at same time. I guess shared_ptr<T> may be forced to behave like T*, if constructed with no-op deleter and no-op (de)allocator? Or may be there is universal smart pointer, which bind

is it better to use shared_ptr.reset or operator =?

I'm trying to wrap my head around the new idioms for C++11. It seems that with shared_ptr at least, there is a substantive difference between using new T() and make_shared<T>(). But what of resetting a shared pointer to point to a new instance of so

Strange Segfault with unique_ptr and shared_ptr

I came accross a strange segfault. The cause actually led me to a bug, but I still don't understand why a segmentation fault is caused here... The code is: #include <memory> int main(int argc, char **arv) { int *i = new int; std::unique_ptr<int&g

Initialize Boost shared_ptr in the constructor

I have a class, which has aboost::asio::io_service object. I want this object stored in a boost::shared_ptr. So my header looks like this ( I got rid of any unnecessary code so it doesn't distract ) class CommandDispatcher { private: boost::shared_pt

Detect if an object belongs to a smart pointer

I have a class which derives from enable_shared_from_this and a method which returns a shared pointer by calling shared_from_this(). I would like to in that method detect if the object is owned by a shared_ptr and if not throw. I tried something like

shared_ptr by reference or by value?

When a function should take a shared_ptr (from boost or C++11 STL), are you passing it by const reference: void foo(const shared_ptr<T>& p) or by value: void foo(shared_ptr<T> p) ? I would prefer the first method because I suspect it to be