make-shared

Errors in std::make_shared() when trying to make shared_ptr?

江枫思渺然 提交于 2019-11-29 14:14:49
(Using Visual Studio 2010) I'm trying to create a shared_ptr of an existing class in my project (class was written a decade before std::shared_ptr existed). This class takes a non-const pointer to another object, it's empty parameter constructor is private. class Foobar { public: Foobar(Baz* rBaz); private: Foobar(); } When I try to create a shared_ptr to it, things don't go well: Baz* myBaz = new Baz(); std::shared_ptr<Foobar> sharedFoo = std::make_shared<Foobar>(new Foobar(myBaz)); On VS2010, this gives me error C2664: 'Foobar::Foobar(const Foobar &)' : cannot convert parameter 1 from

What happens when using make_shared

不羁的心 提交于 2019-11-28 19:14:27
I'm interested if these two lines of code are the same: shared_ptr<int> sp(new int(1)); // double allocation? shared_ptr<int> sp(make_shared<int>(1)); // just one allocation? If this is true could someone please explain why is it only one allocation in the second line? The first case does not perform a double allocation, it performs two allocations, one for the managed object and one for the control block of the shared_ptr . For the second case, cppreference has a good explanation for why std::make_shared usually only performs one memory allocation it says ( emphasis mine going forward ): This

Initializing shared_ptr member variable, new vs make_shared?

旧巷老猫 提交于 2019-11-28 10:21:10
When initializing a shared_ptr member variable: // .h class Customer { public: Customer(); private: std::shared_ptr<OtherClass> something_; } // .cpp Customer(): something_(new OtherClass()) { } vs. Customer(): something_(std::make_shared<OtherClass>()) { } Is the make_shared version allowed? I always seem to see the first version, which is preferred? The only times when make_shared is not allowed are: If you're getting a naked pointer allocated by someone else and storing it in shared_ptr . This is often the case when interfacing with C APIs. If the constructor you want to call is not public

Errors in std::make_shared() when trying to make shared_ptr?

不打扰是莪最后的温柔 提交于 2019-11-28 07:53:18
问题 (Using Visual Studio 2010) I'm trying to create a shared_ptr of an existing class in my project (class was written a decade before std::shared_ptr existed). This class takes a non-const pointer to another object, it's empty parameter constructor is private. class Foobar { public: Foobar(Baz* rBaz); private: Foobar(); } When I try to create a shared_ptr to it, things don't go well: Baz* myBaz = new Baz(); std::shared_ptr<Foobar> sharedFoo = std::make_shared<Foobar>(new Foobar(myBaz)); On

Using make_shared with a protected constructor + abstract interface

倾然丶 夕夏残阳落幕 提交于 2019-11-28 04:08:44
问题 Given an abstract interface and an implementation derived from that interface, where constructors are protected (creation of these objects only being available from a class factory - to implement a DI pattern), how can I make use of make_shared in the factory function? For example: class IInterface { public: virtual void Method() = 0; }; class InterfaceImpl : public IInterface { public: virtual void Method() {} protected: InterfaceImpl() {} }; std::shared_ptr<IInterface> Create() { std:

Is make_shared really more efficient than new?

倖福魔咒の 提交于 2019-11-27 17:41:34
I was experimenting with shared_ptr and make_shared from C++11 and programmed a little toy example to see what is actually happening when calling make_shared . As infrastructure I was using llvm/clang 3.0 along with the llvm std c++ library within XCode4. class Object { public: Object(const string& str) { cout << "Constructor " << str << endl; } Object() { cout << "Default constructor" << endl; } ~Object() { cout << "Destructor" << endl; } Object(const Object& rhs) { cout << "Copy constructor..." << endl; } }; void make_shared_example() { cout << "Create smart_ptr using make_shared..." << endl

weak_ptr, make_shared and memory deallocation

巧了我就是萌 提交于 2019-11-27 15:48:59
问题 A control block of a shared_ptr is kept alive while there is at least one weak_ptr present. If the shared pointer was created with make_shared that implies that the whole memory of the object is kept allocated. (The object itself is properly destructed, but since the control block and the memory for the object were allocated in one chunk, as make_shared does, they can only be deallocated together.) Is my understanding correct? It seems that this behaviour represents a problem, for example in

What happens when using make_shared

随声附和 提交于 2019-11-27 12:07:37
问题 I'm interested if these two lines of code are the same: shared_ptr<int> sp(new int(1)); // double allocation? shared_ptr<int> sp(make_shared<int>(1)); // just one allocation? If this is true could someone please explain why is it only one allocation in the second line? 回答1: The first case does not perform a double allocation, it performs two allocations, one for the managed object and one for the control block of the shared_ptr . For the second case, cppreference has a good explanation for

Can I use boost::make_shared with a private constructor?

喜欢而已 提交于 2019-11-27 06:08:45
问题 Consider the following: class DirectoryIterator; namespace detail { class FileDataProxy; class DirectoryIteratorImpl { friend class DirectoryIterator; friend class FileDataProxy; WIN32_FIND_DATAW currentData; HANDLE hFind; std::wstring root; DirectoryIteratorImpl(); explicit DirectoryIteratorImpl(const std::wstring& pathSpec); void increment(); bool equal(const DirectoryIteratorImpl& other) const; public: ~DirectoryIteratorImpl() {}; }; class FileDataProxy //Serves as a proxy to the WIN32

Is make_shared really more efficient than new?

走远了吗. 提交于 2019-11-27 04:12:56
问题 I was experimenting with shared_ptr and make_shared from C++11 and programmed a little toy example to see what is actually happening when calling make_shared . As infrastructure I was using llvm/clang 3.0 along with the llvm std c++ library within XCode4. class Object { public: Object(const string& str) { cout << "Constructor " << str << endl; } Object() { cout << "Default constructor" << endl; } ~Object() { cout << "Destructor" << endl; } Object(const Object& rhs) { cout << "Copy constructor