unique-ptr

Reassign unique_ptr object with make_unique statements - Memory leak?

和自甴很熟 提交于 2020-07-04 22:01:49
问题 I don't get what following statement would do (specially second line)? auto buff = std::make_unique<int[]>(128); buff = std::make_unique<int[]>(512); Will the second call to make_unique followed by assignment operator will de-allocate memory allocated by first call, or will there be memory leak? Must I have to use buff.reset(new int[512]); ? I've debugged it, but didn't find any operator= being called, nor any destructor be invoked (by unique_ptr ). 回答1: gcc 5.3: #include <memory> extern void

Mix boost::optional and std::unique_ptr

天涯浪子 提交于 2020-07-02 18:21:06
问题 I admit it: I'm in love with the concept of optional. The quality of my code has improved so much ever since I discovered it. Making it explicit whether a variable may or may not be valid is so much better than plain error codes and in-band signaling. It also allows me to not worry about having to read the contract in the documentation, or worrying about whether it's up-to-date: the code itself is the contract. That said, sometimes I need to deal with std::unique_ptr . Objects of this type

make_unique with brace initialization

不羁的心 提交于 2020-06-13 19:08:57
问题 https://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique writes that std::make_unique can be implemented as template<typename T, typename... Args> std::unique_ptr<T> make_unique(Args&&... args) { return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); } This does not work for plain structs with no constructors. Those can be brace-initialized but don't have a non-default constructor. Example: #include <memory> struct point { int x, z; }; int main() { std::make_unique<point>(1, 2)

make_unique with brace initialization

半世苍凉 提交于 2020-06-13 19:07:33
问题 https://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique writes that std::make_unique can be implemented as template<typename T, typename... Args> std::unique_ptr<T> make_unique(Args&&... args) { return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); } This does not work for plain structs with no constructors. Those can be brace-initialized but don't have a non-default constructor. Example: #include <memory> struct point { int x, z; }; int main() { std::make_unique<point>(1, 2)

How to get array size stored in unique_ptr?

牧云@^-^@ 提交于 2020-05-28 18:31:44
问题 if I do: std::unique_ptr<int[]> uparr(new int[1984]); and I pass uparr to somebody without passing the 1984 to them can they see how many elements it has? aka is there equivalent of vector's .size() for unique_ptr of array ? 回答1: No, there isn't. Dynamic arrays are a somewhat defective language feature. You'll essentially always want/need to pass the array length around separately (unless you have some kind of sentinel policy). So you might as well use std::vector (if you don't mind the extra

Factory pattern using unique_ptr in c++

China☆狼群 提交于 2020-05-25 05:31:15
问题 I have an old factory implementation in c++, and I want to use unique pointers instead of raw pointers in it. A minimal example of my code is as follows. I have a base class A , and a derived class B . In main() , I pass 1 to the create method in A , and the type of b1 is now changed to B . #include <iostream> #include <map> class A { public: A() {} virtual void Foo() {} std::map<int, A *> &registerType() { static std::map<int, A *> map_instance; return map_instance; } A *create(int n) {

Factory pattern using unique_ptr in c++

允我心安 提交于 2020-05-25 05:31:10
问题 I have an old factory implementation in c++, and I want to use unique pointers instead of raw pointers in it. A minimal example of my code is as follows. I have a base class A , and a derived class B . In main() , I pass 1 to the create method in A , and the type of b1 is now changed to B . #include <iostream> #include <map> class A { public: A() {} virtual void Foo() {} std::map<int, A *> &registerType() { static std::map<int, A *> map_instance; return map_instance; } A *create(int n) {

What happens to unique_ptr after std::move()?

心已入冬 提交于 2020-05-22 19:20:06
问题 This code is what I want to do: Tony& Movie::addTony() { Tony *newTony = new Tony; std::unique_ptr<Tony> tony(newTony); attachActor(std::move(tony)); return *newTony; } I am wondering if I could do this instead: Tony& Movie::addTony() { std::unique_ptr<Tony> tony(new Tony); attachActor(std::move(tony)); return *tony.get(); } But will *tony.get() be the same pointer or null? I know I could verify, but what is the standard thing for it to do? 回答1: No, you cannot do that instead. Moving the

having difficulties to make a vector of polymorphic objects using shared_ptr and unique_ptr in C++ Visual studio 2019 Cmake project

吃可爱长大的小学妹 提交于 2020-05-16 03:54:08
问题 For an exercise i need to build a vector of polymorphic objects and for some reason both shared and Unique ptr make linkage errors 2019 and 1120 when i use them. i have no option to use the old way of memory allocation with New and Delete so i have to make it work. i tried various of different syntax options for doing this and still with no luck. *note: we use Cmake to bind the project together in visual studio and also we splitting the objects into header and cpp files. here are my objects

Why is unique_ptr::release not defined with [[nodiscard]]?

你。 提交于 2020-05-11 05:37:10
问题 C++17 added [[nodiscard]]. C++20 added the use of [[nodiscard]] on empty methods, e.g. vector::empty() -- maybe, to avoid user confusion with the method clear (i.e. calling empty() accidentally to clear the vector). Why didn't C++20 use this opportunity to add [[nodiscard]] to unique_ptr::release ? Is there a valid reasonable scenario in which one would call unique_ptr::release without taking the returned value? In the same manner of avoiding user confusion (if this was the reason for adding