unique-ptr

using std::unique_ptr with allocators

爱⌒轻易说出口 提交于 2020-01-14 09:42:12
问题 I was trying my hand with allocators this time & felt that there were many chances of leaking the resources. So I thought what if I used std::unique_ptr to handle them. I tried my hand with a std::vector 's allocator. My code goes like this :- // allocator #include <iostream> #include <vector> #include <memory> using namespace std; class X { int x,ID; static int i; public: X() { cout<<"constructing "; ID=++i; cout<<"ID="<<ID<<'\n'; } X(int a) { x=a; cout<<"constructing "; ID=++i; cout<<"ID="<

Why do QString and vector<unique_ptr<int>> appear incompatible here?

风格不统一 提交于 2020-01-13 10:33:35
问题 I'm trying to compile some code, which reduces to this: #include <memory> #include <vector> #include <QString> class Category { std::vector<std::unique_ptr<int>> data; QString name; }; int main() { std::vector<Category> categories; categories.emplace_back(); }; Compiled as is, it results in the following error from g++ and similar for clang++: In file included from /opt/gcc-4.8/include/c++/4.8.2/memory:64:0, from test.cpp:1: /opt/gcc-4.8/include/c++/4.8.2/bits/stl_construct.h: In

Why do QString and vector<unique_ptr<int>> appear incompatible here?

有些话、适合烂在心里 提交于 2020-01-13 10:32:29
问题 I'm trying to compile some code, which reduces to this: #include <memory> #include <vector> #include <QString> class Category { std::vector<std::unique_ptr<int>> data; QString name; }; int main() { std::vector<Category> categories; categories.emplace_back(); }; Compiled as is, it results in the following error from g++ and similar for clang++: In file included from /opt/gcc-4.8/include/c++/4.8.2/memory:64:0, from test.cpp:1: /opt/gcc-4.8/include/c++/4.8.2/bits/stl_construct.h: In

The correct way of returning std::unique_ptr to an object of polymorphic class

被刻印的时光 ゝ 提交于 2020-01-13 07:47:06
问题 Let's say I have the following hierarchy of classes: struct Base { }; struct Derived : public Base { void DoStuffSpecificToDerivedClass() { } }; And the following factory method: std::unique_ptr<Base> factoryMethod() { auto derived = std::make_unique<Derived>(); derived->DoStuffSpecificToDerivedClass(); return derived; // does not compile } The problem is, the return statement does not compile, because std::unique_ptr does not have a copy constructor with covariance support (which makes sense

Trivial raw pointer that self-initializes to nullptr in C++

∥☆過路亽.° 提交于 2020-01-13 05:33:28
问题 I like the new pointer types in C++11, but sometimes I still need a raw pointer. Something that makes me increasingly sad about "raw" types in C++, however, is their habit of initializing as undefined when not given an explicit value. As I use std::shared_ptr<> and the like more often, this need to initialize raw pointers to null feels increasingly brittle and unnecessary. I'm talking about: class foo { ... std::shared_ptr< bar > pb; // Initially null in whatever constructor. std::unique_ptr<

What is the right way to expose resources owned by a class?

会有一股神秘感。 提交于 2020-01-12 12:09:49
问题 Let's say I have a library which has a Document class. An instance of Document can own several instances of Field . Field has multiple subclasses (for example IntegerField and StringField ), and even the API user can subclass it and supply subclass instances to Document (let's say the user is allowed to develop a custom type of data to store in a field). I'd like to expose the Field instances owned by Document through an API in such a way that users can interact with them, but without

What is the right way to expose resources owned by a class?

天大地大妈咪最大 提交于 2020-01-12 12:09:34
问题 Let's say I have a library which has a Document class. An instance of Document can own several instances of Field . Field has multiple subclasses (for example IntegerField and StringField ), and even the API user can subclass it and supply subclass instances to Document (let's say the user is allowed to develop a custom type of data to store in a field). I'd like to expose the Field instances owned by Document through an API in such a way that users can interact with them, but without

Why is there no safe alternative to unique_ptr::operator*()?

守給你的承諾、 提交于 2020-01-12 12:00:13
问题 std::vector has the member function at() as a safe alternative to operator[] , so that bound checking is applied and no dangling references are created: void foo(std::vector<int> const&x) { const auto&a=x[0]; // What if x.empty()? Undefined behavior! const auto&a=x.at(0); // Throws exception if x.empty(). } However, std::unique_ptr lacks the corresponding functionality: void foo(std::unique_ptr<int> const&x) { const auto&a=*x; // What if bool(x)==false? Undefined behavior! } It would be great

Is unique_ptr thread safe?

老子叫甜甜 提交于 2020-01-10 17:26:23
问题 Is unique_ptr thread safe? Is it impossible for the code below to print same number twice? #include <memory> #include <string> #include <thread> #include <cstdio> using namespace std; int main() { unique_ptr<int> work; thread t1([&] { while (true) { const unique_ptr<int> localWork = move(work); if (localWork) printf("thread1: %d\n", *localWork); this_thread::yield(); } }); thread t2([&] { while (true) { const unique_ptr<int> localWork = move(work); if (localWork) printf("thread2: %d\n",

To support move semantics, should function parameters be taken by unique_ptr, by value, or by rvalue?

不问归期 提交于 2020-01-09 16:56:14
问题 One of my function takes a vector as a parameter and stores it as a member variable. I am using const reference to a vector as described below. class Test { public: void someFunction(const std::vector<string>& items) { m_items = items; } private: std::vector<string> m_items; }; However, sometimes items contains a large number of strings, so I'd like to add a function (or replace the function with a new one) that supports move semantics. I am thinking of several approaches, but I'm not sure