shared-ptr

Private constructor and make_shared

夙愿已清 提交于 2019-11-30 23:35:35
I have a singleton class with a private constructor. In the static factory method I do the following: shared_ptr<MyClass> MyClass::GetInstance() { static once_flag onceFlag; call_once(onceFlag, []() { if (_instance == nullptr) _instance.reset(new MyClass()); }); return _instance; } If I use _instance = make_shared<MyClass>(); the code does not compile. My question is: why new can invoke a private constructor but make_shared not? As mentioned, std::make_shared or its component parts don't have access to private members. the call_once and once_flag are un-necessary. They are implicit in c++11

Getting a unique_ptr for a class that inherits enable_shared_from_this

♀尐吖头ヾ 提交于 2019-11-30 23:00:22
Usually I prefer returning unique_ptr from Factories. Recently I came to the problem of returning a unique_ptr for a class that inherits enable_shared_from_this . Users of this class may accidentally cause a call to shared_from_this() , though it is not owned by any shared_ptr , which results with a std::bad_weak_ptr exception (or undefined behavior until C++17, which is usually implemented as an exception). A simple version of the code: class Foo: public enable_shared_from_this<Foo> { string name; Foo(const string& _name) : name(_name) {} public: static unique_ptr<Foo> create(const string&

shared_ptr vs CComPtr

流过昼夜 提交于 2019-11-30 22:45:25
I'm somewhat used to the concept of refcounting through COM and I'm somewhat new to shared_ptr. There are several nice properties with CComPtr that I don't find in shared_ptr, and I'm wondering what are the pattern that prevent missuse of shared_ptr. The AddRef/Release pattern guarantees there is only one refcount per object (the refcount is stored on the object itself), so it's safe, when you have a random pointer to create a CComPtr around it. On the other hand, shared_ptr has a separate refcount pointer, so it's unsafe to create a new shared_ptr on an object (why does the standard provide a

Usage of std::shared_ptr

吃可爱长大的小学妹 提交于 2019-11-30 21:32:56
How can I use std::shared_ptr for array of double? Additionally what are advantages/disadvantages of using shared_ptr. It depends on what you're after. If you just want a resizable array of doubles, go with std::vector<double> Example: std::vector<double> v; v.push_back(23.0); std::cout << v[0]; If sharing the ownership of said array matters to you, use e.g. std::shared_ptr<std::vector<double>> Example: std::shared_ptr<std::vector<double>> v1(new std::vector<double>); v1->push_back(23.0); std::shared_ptr<std::vector<double>> v2 = v1; v2->push_back(9.0); std::cout << (*v1)[1]; Alternatively,

Manually incrementing and decrementing a boost::shared_ptr?

北慕城南 提交于 2019-11-30 21:04:58
Is there a way to manually increment and decrement the count of a shared_ptr in C++? The problem that I am trying to solve is as follows. I am writing a library in C++ but the interface has to be in pure C. Internally, I would like to use shared_ptr to simplify memory management while preserving the ability to pass a raw pointer through the C interface. When I pass a raw pointer through the interface, I would like to increment the reference count. The client will then be responsible to call a function that will decrement the reference count when it no longer needs the passed object. In your

C++ shared_ptr vs. unique_ptr for resource management

只愿长相守 提交于 2019-11-30 20:30:29
I've been mulling over use of unique_ptr vs shared_ptr vs own_solution . I've discounted the latter as I'll almost certainly get it wrong, but I have a problem with both unique_ptr and shared_ptr in that neither captures precisely what I want. I want to create a resource manager which explicitly owns a resource, however I'd like the resource manager to also hand out references to the resource. If I use unique_ptr in the resource manager and hand out raw pointers there's the possibility they could escape elsewhere (though this would be against the class "contract" I suppose). If I use shared

GCC atomic shared_ptr implementation

旧街凉风 提交于 2019-11-30 20:01:57
问题 According to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57250 , GCC 4.9 has support for atomic shared_ptr operations. Using GCC 4.9.2, I'm able to compile a program that uses atomic shared_ptr . The -mcx16 flag is required, as the GCC implementation on x86_64 apparently requires cmpxchg16b , which makes sense as I would assume that an atomic operation on a shared_ptr would require atomically updating both the pointer itself and the reference count at the same time. However, when I try to

Unable to use custom allocator with allocate_shared/make_shared

房东的猫 提交于 2019-11-30 19:17:15
In my C++11 program, I use shared_ptr<T> for some objects which are actively created and deleted. It so happened that standard allocator with operator new is a bottleneck, so I want to create my own one, which will allocate a bunch of memory at once and then give to to make_shared on demand. Unfortunatelly, this is the first time I write an allocator and I have no idea why GCC is unable to compile the following code: #include <memory> class MyAlloc { public: typedef char* pointer; typedef const char* const_pointer; typedef char value_type; char* allocate(size_t len) { return new char[len]; }

Private constructor and make_shared

社会主义新天地 提交于 2019-11-30 18:41:22
问题 I have a singleton class with a private constructor. In the static factory method I do the following: shared_ptr<MyClass> MyClass::GetInstance() { static once_flag onceFlag; call_once(onceFlag, []() { if (_instance == nullptr) _instance.reset(new MyClass()); }); return _instance; } If I use _instance = make_shared<MyClass>(); the code does not compile. My question is: why new can invoke a private constructor but make_shared not? 回答1: As mentioned, std::make_shared or its component parts don't

c++ create shared_ptr to stack object

隐身守侯 提交于 2019-11-30 18:35:05
In my method a Player object is created like: Player player(fullName,age); My teacher gave us a piece of code with a constructor that takes a shared_ptr to a player object. //constructor of the class SomeClass(const std::shared_ptr<Socket> client, std::shared_ptr<Player> player) Lets say we want to call the constructor of SomeClass and pass the player object we created on stack. Is it ever safe/possible/good to create a shared_ptr from a stack object? To make the question more understandable lets say we have two big code projects and we want to merge them so a method from one project is called