Linked Questions
17 questions linked to/from Advantages of using std::make_unique over new operator
207 votes
4 answers
102k views
Differences between std::make_unique and std::unique_ptr with new
Does std::make_unique have any efficiency benefits like std::make_shared? Compared to manually constructing std::unique_ptr: std::make_unique<int>(1); // vs std::unique_ptr<int>(...
1 vote
1 answer
155 views
Assignment of unique_ptr inside member initializer list [duplicate]
Say if I have this definition: class A { A(); std::unique_ptr<B> m_b; }; class B {}; Is there a difference if I initialize A like: A::A() : m_b(new B()) {} vs. A::A() : m_b(std::...
136 votes
6 answers
46k views
Why use std::make_unique in C++17?
As far as I understand, C++14 introduced std::make_unique because, as a result of the parameter evaluation order not being specified, this was unsafe: f(std::unique_ptr<MyClass>(new MyClass(...
9 votes
2 answers
4k views
Convert unique_ptr<Derived> to unique_ptr<Base>
A simple code class Base {}; class Derived : Base {}; unique_ptr<Base> Create() { unique_ptr<Base> basePtr = make_unique<Derived>(); // compile error return basePtr; } ...
2 votes
3 answers
5k views
C++: How to push unique_ptr to deque?
In my project I need container that holds smart pointers to an data unit instances. I write the class (simple example): template <typename T> class Queue { public: void push(const T & ...
4 votes
3 answers
2k views
Containers vs Smart pointers in C++
How can I decide when choosing between std::containers (std::vector or std::array) and smart pointers pointing to arrays I know containers are objects for memory managment. They are exception safe ...
2 votes
3 answers
790 views
Is there a transparent way of using unique_ptr in std containers?
Is there a transparent way of using std::unique_ptr in containers? #include <iostream> ...
4 votes
1 answer
1k views
C++ working with objects on heap
I am currently learning C++, coming from C#/Java background, using visual studio 2017. I have a question in regards to creating objects on heap and referencing them properly down the road. So far I ...
2 votes
1 answer
2k views
std::make_unique with placement new
I'm trying to define a Data object that contains its size, followed by size bytes of data. Something like: struct Data { size_t size; char data[1]; static void* operator new( std::...
0 votes
2 answers
1k views
Is there a way to let "make_unique< T[ ] >" can forward arguments to constructor of T?
I have some objects of a same class in an array that was pointed by a unique_ptr, and each object can only be created with the explicit constructor, since a few of arguments must be passed to the ...
0 votes
0 answers
853 views
Why does using new with std::unique_ptr in this case cause a crash?
I was looking at this Arduino code sample (https://buger.dread.cz/simple-esp8266-https-client-without-verification-of-certificate-fingerprint.html): std::unique_ptr<BearSSL::WiFiClientSecure>...
0 votes
3 answers
243 views
Bus error in printf for printing the member of structure
struct abc { float b; }; abc* xyz; xyz->b = 10; printf("%f", xyz->b); error at printf. What is the reason? How can I solve this?
3 votes
2 answers
314 views
How to know when to call delete and when delete[] in C++?
I was implementing classes my_unique_ptr and my_shared_ptr that mimic the standard library smart pointers std::unique_ptr and std::shared_ptr to get to know it better. When implementing the ...
1 vote
2 answers
205 views
Difficulty understanding c++ polymorphism
What I am trying to achieve is creating a superclass array of subclass objects. In this particular test I'm working on, I want to have an animal array that has some dog objs and some cat objs while ...
1 vote
1 answer
184 views
Using std::make_unique with the GetProfileBinary function call
I have seen this answer (Advantages of using std::make_unique over new operator) where it states: Don't use make_unique if you need a custom deleter or are adopting a raw pointer from elsewhere. ...