unique-ptr

Although unique_ptr guaranteed to store nullptr after move, it still is pointing to the object?

我与影子孤独终老i 提交于 2019-12-13 02:55:14
问题 I've tested following code with GCC 5.2 (C++11): #include <iostream> #include <memory> struct Foo { Foo() { std::cout << "Foo::Foo\n"; } ~Foo() { std::cout << "Foo::~Foo\n"; } void bar() { std::cout << "Foo::bar\n"; } }; void f(const Foo &) { std::cout << "f(const Foo&)\n"; } int main() { std::unique_ptr<Foo> p1(new Foo); // p1 owns Foo if (p1) p1->bar(); { //p1->bar(); std::unique_ptr<Foo> p2(std::move(p1)); // now p2 owns Foo f(*p2); p1->bar(); if(p1==nullptr) { std::cout<<"NULL"<<std::endl

Is there a transparent way of using unique_ptr in std containers?

半城伤御伤魂 提交于 2019-12-12 20:38:03
问题 Is there a transparent way of using std::unique_ptr in containers? #include <iostream> #include <memory> #include <map> struct method { virtual ~method() { std::cout << "f\n"; }; }; typedef std::unique_ptr<method> MPTR; std::map<int, MPTR> tbl; void insert(int id, method *m) { tbl.insert({id,std::unique_ptr<method>(m)}); }; void set(int id, method *m) { tbl[id] = std::unique_ptr<method>(m); }; int main(int argc, char **argv) { insert(1,new method()); set(1,new method()); return 0; } I'd like

Using unique_ptr with gsl_vector

五迷三道 提交于 2019-12-12 18:35:27
问题 One of my favorite aspects of unique_ptr is the automatic memory management it provides. I would like to use unique_ptr with something like a GSL vector. However, gsl_vector has its own mechanism for freeing memory ( gsl_vector_free ). Is there a way to force the unique pointer to use GSL's vector freeing method? Valgrind (correctly) reports a mismatched use of malloc/delete when creating a unique_ptr<gsl_vector> below: #include <memory> #include <gsl/gsl_vector.h> void mem_gsl() { gsl_vector

access and move unique_ptr in a function call

半腔热情 提交于 2019-12-12 16:19:10
问题 I have a segment similar to the following. struct derive : base{ derive(unique_ptr ptr): base{func(ptr->some_data), std::move(ptr)}{} }; In theory, it should work. But since the compiler (vs2015) does not strictly follow the standard, the order of func(ptr->some_data), std::move(ptr) is undefined, i.e. ptr may be moved before accessed. So my problem is how to make this segment work as expected? Complete code like this: #include <memory> struct base { virtual ~base() = 0 {} protected: base(std

Can I throw a unique_ptr?

自古美人都是妖i 提交于 2019-12-12 15:56:09
问题 I have started using C++ 11 and in particular using unique_ptr liberally to make code exception-safe and ownership more readable. This has generally worked well until I wanted to throw a unique_ptr. I have error code (thrown many places, caught in a single place) that creates complex state. Since the ownership of that dynamically allocated memory logically is being transferred from the thrower to the catcher, unique_ptr seemed like the appropriate type to indicate that and make it clear that

Conversion to non-scalar type with std c++11 smart pointer

↘锁芯ラ 提交于 2019-12-12 13:28:18
问题 I am currently playing around with openscenegraph and it uses its own smart pointer. But I want to use the std c++11 smart pointer. now this is the working example code osg::ref_ptr<osg::Uniform> SineUniform = new osg::Uniform( "Sine", 0.0f ); but when I do something like this std::unique_ptr<osg::Uniform> SineUniform = new osg::Uniform( "Sine", 0.0f ); Then I get the following error message error: conversion from 'osg::Uniform*' to non-scalar type 'std::unique_ptr' requested Any idea what is

pimpl: Avoiding pointer to pointer with pimpl

烈酒焚心 提交于 2019-12-12 13:07:44
问题 In this question I asked "pimpl: shared_ptr or unique_ptr" I've been convinced that the proper usage of the pimpl idiom is to use a unique_ptr , not a shared_ptr . It should act to the user as if there is no pointer at all, whereas quite clearly the shared_ptr introduces aliasing upon copying, which definitely acts like a pointer. So, lets say a user wants to create a shared_ptr to my pimpl object (say if they want actually want multiple aliases to it). For example: shared_ptr<my_pimpl> p(new

deep copy and dynamic cast unique_ptr

主宰稳场 提交于 2019-12-12 10:25:00
问题 Suppose I have a class like the following: class A { virtual ~A(); ... } class B : public A { ... } class C : public A { ... } I also have a vector of unique_ptr which is declared this way: std::vector<std::unique_ptr<A>> vec; Assume vec is populated with unique_ptr to objects of derived class. What should I do if I want a deep copy of any of the vector elements, either b or c, and let a base class unique_ptr pointing to it? Originally I was doing things like std::unique_ptr<A> tmp = std:

Should use unique_ptr to more easily implement “move” semantics?

筅森魡賤 提交于 2019-12-12 08:59:34
问题 Edit: made Foo and Bar a little less trivial, and direct replacement with shared_ptr<> more difficult. Should unique_ptr<> be used as an easier way to implement move semantics? For a class like class Foo { int* m_pInts; bool usedNew; // other members ... public: Foo(size_t num, bool useNew=true) : usedNew(useNew) { if (usedNew) m_pInts = new int[num]; else m_pInts = static_cast<int*>(calloc(num, sizeof(int))); } ~Foo() { if (usedNew) delete[] m_pInts; else free(m_pInts); } // no copy, but

How do we return a unique_pointer member from a member function?

北城以北 提交于 2019-12-12 07:59:01
问题 I have a base class with a pointer member. I would have to make an educated guess to determine whether it should be an unique_ptr or a shared_ptr . None of them seems to solve my particular use case. class Base { public: Base(): pInt(std::unique_ptr<int>(new int(10))) {}; virtual std::unique_ptr<int> get() = 0; //Base(): pInt(std::shared_ptr<int>(new int(10))) {}; // Alternate implementation //virtual std::shared_ptr<int> get() = 0; // Alternate implementation private: std::unique_ptr<int>