make-shared

Does std::make_shared() use custom allocators?

余生颓废 提交于 2019-12-03 10:11:45
Consider this code : #include <memory> #include <iostream> class SomeClass { public: SomeClass() { std::cout << "SomeClass()" << std::endl; } ~SomeClass() { std::cout << "~SomeClass()" << std::endl; } void* operator new(std::size_t size) { std::cout << "Custom new" << std::endl; return ::operator new(size); } void operator delete(void* ptr, std::size_t size) { std::cout << "Custom delete" << std::endl; ::operator delete(ptr); } }; int main() { std::shared_ptr<SomeClass> ptr1(new SomeClass); std::cout << std::endl << "Another one..." << std::endl << std::endl; std::shared_ptr<SomeClass> ptr2

was raw-pointer constructor of shared_ptr a mistake?

自古美人都是妖i 提交于 2019-12-03 07:02:22
In hindsight, given make_shared , would shared_ptr have a constructor that takes a raw pointer had it been introduced with C++11? Are there strong arguments or use cases in favor of this constructor? It would have avoided the well documented pitfall of exception-safety and the memory allocation/performance advantage of using make_shared . I believe another advantage of requiring shared_ptr construction via make_shared would be that it could be a single pointer under the hood, lowering its memory use and making things like atomic_compare_exchange a lot simpler (and possibly more efficient).

std::make_shared() change in C++17

巧了我就是萌 提交于 2019-12-03 04:25:13
In cppref , the following holds until C++17: code such as f(std::shared_ptr<int>(new int(42)), g()) can cause a memory leak if g gets called after new int(42) and throws an exception, while f(std::make_shared<int>(42), g()) is safe, since two function calls are never interleaved. I'm wondering which change introduced in C++17 renders this no longer applicable. The evaluation order of function arguments are changed by P0400R0 . Before the change, evaluation of function arguments are unsequenced relative to one another. This means evaluation of g() may be inserted into the evaluation of std:

C++ Supply initializer-list constructor for class template

痞子三分冷 提交于 2019-12-01 06:06:53
I have a class template Templ with template parameter T, and the Templ class has a data member of type T, called obj. I wrote a variadic constructor template which forwards the arguments to obj's constructor: template <class T> class Templ { public: template <class... Args> explicit Templ (Args&&... args) : obj (std::forward<Args>(args)...) { } private: T obj; }; Now I realized that type T may be a class with an init-list constructor, and I want it to be accessible via Templ. So I checked what std::list::emplace and std::make_shared do. They have a variadic function like mine, but they don't

C++ Supply initializer-list constructor for class template

匆匆过客 提交于 2019-12-01 04:39:58
问题 I have a class template Templ with template parameter T, and the Templ class has a data member of type T, called obj. I wrote a variadic constructor template which forwards the arguments to obj's constructor: template <class T> class Templ { public: template <class... Args> explicit Templ (Args&&... args) : obj (std::forward<Args>(args)...) { } private: T obj; }; Now I realized that type T may be a class with an init-list constructor, and I want it to be accessible via Templ. So I checked

Does enable_shared_from_this and make_shared provide the same optimization

家住魔仙堡 提交于 2019-11-30 13:57:46
问题 As I understand make_shared<T>(...) may provide some memory allocation optimization (it may allocate reference counter within same memory block as instance of class T). Do enable_shared_from_this provides the same optimization? So: class T : std::enable_shared_from_this<T> {}; ... auto t = std::shared_ptr<T>(new T); Is the same as: class T {}; ... auto t = std::make_shared<T>(); If not take in account sizeof(T). 回答1: Do enable_shared_from_this provides the same optimization? So: No. As you

Does enable_shared_from_this and make_shared provide the same optimization

心不动则不痛 提交于 2019-11-30 09:00:05
As I understand make_shared<T>(...) may provide some memory allocation optimization (it may allocate reference counter within same memory block as instance of class T). Do enable_shared_from_this provides the same optimization? So: class T : std::enable_shared_from_this<T> {}; ... auto t = std::shared_ptr<T>(new T); Is the same as: class T {}; ... auto t = std::make_shared<T>(); If not take in account sizeof(T). Do enable_shared_from_this provides the same optimization? So: No. As you can see from the wording in the standard, enable_shared_from_this<T> has a weak_ptr<T> data member. That adds

C++ make_shared not available

柔情痞子 提交于 2019-11-30 05:42:37
问题 While I have std::tr1::shared_ptr<T> available in my compiler, I don't have make_shared . Can someone point me to a proper implementation of make_shared ? I see that I need to use varargs to provide arguments to constructor of T. But I don't have variadic templates available in my compiler as well. 回答1: If your compiler don't give an implementation of make_shared and you can't use boost, and you don't mind the lack of single-allocation optimization both for the object and the reference

How to pass deleter to make_shared?

穿精又带淫゛_ 提交于 2019-11-30 00:26:28
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_shared or make_unique to avoid some error prone. If we like to use a smart pointer class, like shared_ptr , we can construct one like, shared_ptr<int> p(new int(12)); Also we would like to pass a custom deleter to smart pointer classes, shared_ptr<int> p(new int(12), deleter); On the other hand, if we like to use make_shared to allocate, for ex. int ,

Can you allocate an array with something equivalent to make_shared?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-29 16:22:48
问题 buffer = new char[64]; buffer = std::make_shared<char>(char[64]); ??? Can you allocate memory to an array using make_shared<>() ? I could do: buffer = std::make_shared<char>( new char[64] ); But that still involves calling new, it's to my understanding make_shared is safer and more efficient. 回答1: The point of make_shared is to incorporate the managed object into the control block of the shared pointer, Since you're dealing with C++11, perhaps using a C++11 array would satisfy your goals?