unique-ptr

Exception safety and make_unique

大兔子大兔子 提交于 2019-12-28 04:13:04
问题 Just to clarify, using make_unique only adds exception safety when you have multiple allocations in an expression, not just one, correct? For example void f(T*); f(new T); is perfectly exception safe (as far as allocations and stuff), while void f(T*, T*); f(new T, new T); is not, correct? 回答1: Not only when you have multiple allocations, but whenever you can throw at different places. Consider this: f(make_unique<T>(), function_that_can_throw()); Versus: f(unique_ptr<T>(new T), function_that

unique_ptr custom storage type example?

纵饮孤独 提交于 2019-12-24 03:06:53
问题 Howard Hinnant explained that unique_ptr can also use a custom storage type . He mentions as an example " shared memory ". He only gives the rough idea (which is fine for a quick intro). But can anyone complete his example for a "custom storage type" (be it shared memory or not)? To support placing unique_ptr into shared memory, the custom deleter can contain a custom pointer type (typically not a real pointer in shared memory applications). One simply places a nested type called pointer

Serializing std::vector of unique_ptr using boost::serialization fails on linux

≯℡__Kan透↙ 提交于 2019-12-23 20:16:35
问题 I am encountering an issue with using boost serialization of a std::vector of std::unique_ptr's. The type of the unique_ptr does not matter, and in the below example, integers are used: #include <boost/serialization/unique_ptr.hpp> #include <boost/serialization/vector.hpp> #include <boost/archive/xml_iarchive.hpp> #include <boost/archive/xml_oarchive.hpp> #include <fstream> namespace boost { namespace serialization { template <class Archive> void serialize(Archive& ar, std::vector<unique_ptr

Moving a vector of unique_ptr<T> [duplicate]

梦想与她 提交于 2019-12-23 16:36:59
问题 This question already has answers here : Should a move constructor take a const or non-const rvalue reference? (4 answers) Closed 4 years ago . So I have a situation where I need to store a vector of an abstract type, as I understand this requires the usage of a vector of unique_ptrs or similar. So in order to move about instances of the class which contains the vector of unique_ptrs, I need to define a move constructor which I have done. However as demonstrated by the example below, this

Why is there no [] operator for std::shared_ptr? [duplicate]

我只是一个虾纸丫 提交于 2019-12-23 09:29:11
问题 This question already has an answer here : Why isn't there a std::shared_ptr<T[]> specialisation? (1 answer) Closed 4 years ago . I wonder what the rationale is behind the fact, that std::shared_ptr does not define the [] operator for arrays. In particular why does std::unique_ptr feature this operator but not std::shared_ptr ? 回答1: std::unique_ptr only defines operator[] in a specialization for arrays: std::unique_ptr<T[]> . For non-array pointers, the operator[] doesn't make much sense

Could an optimizing compiler remove all runtime costs from std::unique_ptr?

时间秒杀一切 提交于 2019-12-23 09:27:42
问题 Reading about std::unique_ptr at http://en.cppreference.com/w/cpp/memory/unique_ptr, my naive impression is that a smart enough compiler could replace correct uses of unique_ptr with bare pointers and just put in a delete when the unique_ptr s get destroyed. Is this actually the case? If so, do any of the mainstream optimizing compilers actually do this? If not, would it be possible to write something with some/all of unique_ptr s compile-time safety benefits that could be optimized to have

Why can't I std::move std::unique_ptrs between std::sets?

…衆ロ難τιáo~ 提交于 2019-12-23 09:16:12
问题 I really want to move some unique_ptr s from one std::set into another: #include <memory> #include <algorithm> #include <set> int main() { std::set<std::unique_ptr<int>> a; std::set<std::unique_ptr<int>> b; a.insert({0, std::unique_ptr<int>(new int(42))}); std::move(a.begin(), a.end(), std::inserter(b, b.end())); } However, my GCC 4.8.5 on CentOS 7 is distinctly unhappy: [root@localhost ~]# g++ test.cpp -std=c++11 -o test In file included from /usr/include/c++/4.8.2/set:60:0, from test.cpp:2:

Valgrind shows memory leak in std::make_unique

我的未来我决定 提交于 2019-12-23 06:48:10
问题 I'm using Valgrind to check for memory leaks. Unfortunately I get a Leak_DefinitelyLost warning. Attached is a simplified version of my code that reproduces the error: #include <iostream> #include <vector> #include <memory> #include <unordered_map> using namespace std; class Base{ public: explicit Base(double a){ a_ = a; } virtual void fun() = 0; protected: double a_; }; class Derived_A : public Base{ public: Derived_A(double a, vector<double> b, vector<double> c): Base(a), b_{b}, c_{c}{ }

Smart way to construct class member std::vector<std::unique_ptr<AClass> >

我与影子孤独终老i 提交于 2019-12-23 02:03:52
问题 This question combines unique_ptr as class member and move semantics fail to compile with clang and C++ std::vector in constructor. My goal is to construct a wrapper struct V_wrapper{ std::vector<std::unique_ptr<AClass> > vec; V_wrapper(std::vector<std::unique_ptr<AClass> > v) : vec{std::move(v)} {} }; Unfortunately this code does not compile, because the compiler (clang Apple LLVM version 4.2) attempts to copy construct the vector v which is not supported. On the other hand, if I design an

How do move semantics work with unique_ptr?

谁说胖子不能爱 提交于 2019-12-22 09:27:56
问题 I was experimenting with using unique_ptr and wrote some simple code to check how it works with move semantics. #include <iostream> #include <vector> using namespace std; class X { public: X(){} ~X() { cout << "Destructor X" << endl; } void Print() { cout << "X" << endl; } }; int main() { unique_ptr<X> ptr(new X()); ptr->Print(); vector<unique_ptr<X>> v; v.push_back(move(ptr)); ptr->Print(); v.front()->Print(); return 0; } The output is as follows: X X X Destructor X My expectation was that