unique-ptr Questions

3

Solved

How to properly resize a vector of unique_ptr vectors in one line without gcc giving a compilation error regarding a deleted function? vector<vector<unique_ptr<A> >> a; a.resize(...
Manipular asked 27/11, 2015 at 8:27

1

The proposal for shared_ptr<T[N]> has this paragraph: https://www.open-std.org/JTC1/SC22/WG21/docs/papers/2014/n3920.html The unfortunate occurence of unique_ptr having lost its support for ...
Unquestioned asked 24/5, 2024 at 0:41

1

Solved

Looking at the assembly output of the following simple C++ function: #include <memory> int square(std::unique_ptr<int> num) { return *num * *num; } Gcc and clang emit the follow...
Rasla asked 23/4, 2024 at 15:59

6

Solved

Creating an object and giving ownership to a container using a unique_ptr is no problem. How would one remove an element by raw pointer? std::set<std::unique_ptr<MyClass>> mySet; MyCl...
Xerosere asked 1/8, 2011 at 22:40

3

Solved

I am storing the ownership of some objects inside an unordered_set, using unique_ptrs. But I don't know a good way to erase one of them from the set, when the time comes. Code looks something like...
Kodok asked 14/2, 2020 at 4:57

5

Solved

Edit: Thanks, amazing help, as always :) I can't find the solution to this, I have an unique_ptr to a base class, that has the data of a derived class, and I want to set it's type to the derived c...
Phineas asked 11/11, 2015 at 11:24

4

Solved

Given a struct: struct S { int x; int y; } Why the Standard allows us to do this: std::vector<S> vec; vec.emplace_back(1, 2); But does not allow to do this: auto ptr = std::make_uniq...
Inclined asked 12/7, 2016 at 7:51

1

This code compiles with MSVC from VS 2022 in C++20 mode. It failes in C++23 mode. (/std:c++latest) #include <memory> struct A; struct B { B(); ~B(); std::unique_ptr<A> ptr{nullptr...
Sverdlovsk asked 22/2, 2024 at 11:29

3

Solved

#include <iostream> #include <memory> // unique_ptr using namespace std; int main() { std::unique_ptr<char*> char_ptr; char_ptr = (char*)"anisha"; return 0; } I want to as...
Decamp asked 27/11, 2018 at 6:44

5

Solved

This question is about owning pointers, consuming pointers, smart pointers, vectors, and allocators. I am a little bit lost on my thoughts about code architecture. Furthermore, if this question ha...
Medawar asked 27/5, 2019 at 10:36

4

Solved

I am trying to really move from c++98 to c++11 and newer. I have wrapped my head over most of the new stuff but I am still not sure about the correct usage of unique_ptr. Consider the example belo...
Pedicle asked 4/3, 2017 at 11:31

3

Solved

Given the common situation where the lifespan of an owned object is linked to its owner, I can use a unique pointer one of 2 ways . . It can be assigned: class owner { std::unique_ptr<someOb...
Zackaryzacks asked 17/4, 2013 at 13:29

5

Solved

Does C++11 standard library provide any utility to convert from a std::shared_ptr to std::unique_ptr, or vice versa? Is this safe operation?
Germaun asked 17/6, 2016 at 14:58

5

Solved

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...
Moralize asked 26/3, 2014 at 13:21

2

Solved

Apparently today, MSVC is trying its best to convince me to switch to clang. But I won't give up. Earlier I asked this question wondering how to declare std::make_unique as a friend of my class. I...
Chemoreceptor asked 24/11, 2015 at 23:25

1

Solved

I know that std::unique_ptr doesn't guarantee memory safety and especially it is true in the case of circular dependency. However, in my case, I can't see it. Child contain Parent(or SecondChild in...
Phraseograph asked 2/7, 2023 at 20:57

7

Solved

How do I implement a copy constructor for a class that has a unique_ptr member variable? I am only considering C++11.
Almonte asked 16/4, 2013 at 6:21

3

Solved

It seems an easy way to circumvent a unique_ptr is to use a pointer to the unique_ptr object. And that's not difficult. So using the unique_ptr is sort of a gentleman's agreement and not really sup...
Reena asked 25/9, 2018 at 11:2

1

Solved

I have a follow-up question to this one: Move unique_ptr: reset the source vs. destroy the old object For a quick summary of the original question, there is this sample code on cppreference: struct...
Wolverhampton asked 8/4, 2023 at 15:39

1

My question is about the example in https://en.cppreference.com/w/cpp/memory/unique_ptr struct List { struct Node { int data; std::unique_ptr<Node> next; }; std::unique_ptr<Node&gt...
Hedrick asked 8/4, 2023 at 9:45

1

Solved

When wrapping C-like APIs that use pointers on undefined structures, I generally go to std::unique_ptr with a custom deleter to handle resources. Occasionally, the C API will attribute integer IDs ...
Corkboard asked 25/3, 2023 at 8:57

2

I have a type declared as: using Buffer = std::unique_ptr<std::array<uint8_t, N>>; I also have a template function declared as: template<typename Buffer> bool temp_func() { // d...
Taka asked 25/2, 2023 at 23:6

8

Solved

How can I pass a std::unique_ptr into a function? Lets say I have the following class: class A { public: A(int val) { _val = val; } int GetVal() { return _val; } private: int _val; }; The...
Obi asked 18/6, 2015 at 2:22

2

Solved

From Is std::unique_ptr required to know the full definition of T?, I know that if a class A has a member unique_ptr<T>, then T shall be a complete type in destructor ~A(). However, I came ac...
Meredithmeredithe asked 10/2, 2023 at 8:36

5

Solved

I have tried the following: std::function<void ()> getAction(std::unique_ptr<MyClass> &&psomething){ //The caller given ownership of psomething return [psomething](){ psome...
Grimsby asked 23/11, 2011 at 2:11

© 2022 - 2025 — McMap. All rights reserved.