unique-ptr

Why it is not possible to use an initializer_list to initialize a vector of unique_ptr's? [duplicate]

南笙酒味 提交于 2021-02-16 08:57:38
问题 This question already has answers here : initializer_list and move semantics (8 answers) Closed 6 years ago . I'm wondering why initializer_list doesn't work with unique_ptr: std::vector<std::unique_ptr<int>> vptr = {std::make_unique<int>(1), std::make_unique<int>(2)}; do not compile. However: std::vector<std::unique_ptr<int>> vptr(2); vptr[0] =std::make_unique<int>(1); vptr[1] =std::make_unique<int>(2); compile. and std::vector<int*>vint={new int{1},new int{2}}; or std::vector<std::shared

Why it is not possible to use an initializer_list to initialize a vector of unique_ptr's? [duplicate]

一笑奈何 提交于 2021-02-16 08:55:29
问题 This question already has answers here : initializer_list and move semantics (8 answers) Closed 6 years ago . I'm wondering why initializer_list doesn't work with unique_ptr: std::vector<std::unique_ptr<int>> vptr = {std::make_unique<int>(1), std::make_unique<int>(2)}; do not compile. However: std::vector<std::unique_ptr<int>> vptr(2); vptr[0] =std::make_unique<int>(1); vptr[1] =std::make_unique<int>(2); compile. and std::vector<int*>vint={new int{1},new int{2}}; or std::vector<std::shared

Use std::vector<double> to access data managed by std::unique_ptr<double[2]>

谁说我不能喝 提交于 2021-02-08 05:17:36
问题 I have a complex class, that holds a big block of double[2] -type data managed by a smart pointer like: std::unique_ptr<double[2]> m_data; I cannot change the type of the data structure. I am using a library that gives me a function with the following signature: bool func_in_lib(std::vector<double>& data, double& res) . I cannot change the signature of this function. I want to pass the data managed by the unique_ptr to the function expecting a vector<double>& without breaking the connection

std::unique_ptr usage

China☆狼群 提交于 2021-02-05 19:32:06
问题 std::unique_ptr<int> p1(new int); std::unique_ptr<int> p2(new int); p2=p1; It seems here that p1 is no longer "unique" since p2 refer to it also It is legal c++ ? Does unique_ptr have copy_semantics ? If no, and if it has only move semantics, is p1 set to NULL after assign it to p2 ? EDIT: ok so the correct version is p2=std::move(p1) According to that, after this assign, p1 is not valid ? And the difference with auto_ptr is here? it is more safe to explictly specfiy transfer of ownership

Returning unique_ptr from functions

旧巷老猫 提交于 2021-01-29 05:35:47
问题 unique_ptr<T> does not allow copy construction, instead it supports move semantics. Yet, I can return a unique_ptr<T> from a function and assign the returned value to a variable. #include <iostream> #include <memory> using namespace std; unique_ptr<int> foo() { unique_ptr<int> p( new int(10) ); return p; // 1 //return move( p ); // 2 } int main() { unique_ptr<int> p = foo(); cout << *p << endl; return 0; } The code above compiles and works as intended. So how is it that line 1 doesn't invoke

When a unique_ptr is deallocated?

那年仲夏 提交于 2021-01-27 16:11:58
问题 In this code: void f(std::unique_ptr<int> q) { } void g() { std::unique_ptr<int> p{new int{42}}; f(std::move(p)); } At which line p is deallocated? I'd say at the exit of the f function because it was moved there using std::move, but I'm not sure nor confident about this answer. 回答1: At which line p is deallocated? At the end of the scope where it was declared i.e the function g in this case. That is when objects with automatic storage are destroyed, and their memory deallocated. The integer

How to initialize elements of an array managed by a unique_ptr?

白昼怎懂夜的黑 提交于 2021-01-27 07:07:26
问题 We know that Resource Acquisition is Initialization (RAII), I looked for the syntax to initialize an array of objects that have parameters (with no default params), managed by unique_ptr but I did not find any example, there is one in Cppreference constructing int int size = 10; std::unique_ptr<int[]> fact(new int[size]); How could I write like this: class Widget { Widget(int x, in y):x_(x),y_(y) {} int x_,y_; }; std::unique_ptr<Widget[]> fact(new Widget[size]); 回答1: Following the last answer

Access elements of vector<std::unique_ptr<T> > using an iterator?

六月ゝ 毕业季﹏ 提交于 2021-01-24 08:13:03
问题 I have a class member variable as vector<std::unique_ptr<T> > v; and a member function where I want to use a unique_ptr element of v "addressed" by an iterator argument. Which one is better? void mem_fun(vector<std::unique_ptr<T> >::iterator it) { std::unique_ptr<T> p; p = std::move(*it); ... } Or void mem_fun(vector<std::unique_ptr<T> >::iterator it) { std::unique_ptr<T>& p = *it; ... } From what I know, it seems the second way just kind of violates the "uniqueness" of unique_ptr . But can

Access elements of vector<std::unique_ptr<T> > using an iterator?

元气小坏坏 提交于 2021-01-24 08:04:57
问题 I have a class member variable as vector<std::unique_ptr<T> > v; and a member function where I want to use a unique_ptr element of v "addressed" by an iterator argument. Which one is better? void mem_fun(vector<std::unique_ptr<T> >::iterator it) { std::unique_ptr<T> p; p = std::move(*it); ... } Or void mem_fun(vector<std::unique_ptr<T> >::iterator it) { std::unique_ptr<T>& p = *it; ... } From what I know, it seems the second way just kind of violates the "uniqueness" of unique_ptr . But can

Access elements of vector<std::unique_ptr<T> > using an iterator?

荒凉一梦 提交于 2021-01-24 08:04:05
问题 I have a class member variable as vector<std::unique_ptr<T> > v; and a member function where I want to use a unique_ptr element of v "addressed" by an iterator argument. Which one is better? void mem_fun(vector<std::unique_ptr<T> >::iterator it) { std::unique_ptr<T> p; p = std::move(*it); ... } Or void mem_fun(vector<std::unique_ptr<T> >::iterator it) { std::unique_ptr<T>& p = *it; ... } From what I know, it seems the second way just kind of violates the "uniqueness" of unique_ptr . But can