shared-ptr

Why shared pointer assignment does 'swap'?

一世执手 提交于 2019-12-11 02:21:59
问题 As i understand when assigned shared ptr should behave like: a) if (--this->count == 0) { release this->count and this->obj } b) this->count = r->count, this->obj = r->obj; What boost does is just shared_ptr & operator=( shared_ptr const & r ) BOOST_NOEXCEPT { this_type(r).swap(*this); return *this; } So what is the magic behind swap and why this works? 回答1: a) if (--this->count == 0) { release this->count and this->obj } No, a shared_ptr keeps two counts, one for the object and one for the

shared_from_this throws bad_weak_ptr with boost::asio

懵懂的女人 提交于 2019-12-11 01:25:21
问题 First, I have read all related questions listed. They say, "you must have an existing shared_ptr to this before you can use shared_from_this." As far as I can see, there is no way I am violating that condition. I create the instance of Foo as a shared_ptr and enforced that it is always created as a shared_ptr. I then, stored the shared_ptr in a collection. Yet, I still get the bad_weak_ptr exception when shared_from_this is called. #pragma once #include <memory> #include <vector> //----------

Lifetime of return value in comma separated statements

情到浓时终转凉″ 提交于 2019-12-11 01:19:47
问题 Is the order of execution of the three commented lines below guaranteed? struct S { S() { /* called 1st */ } ~S() { /* called 3rd */ } }; boost::shared_ptr<S> f() { return boost::shared_ptr<S>(new S); } int second() { return 0; /* called 2nd */ } int test() { return (f(), second()); } With my compiler, the shared_ptr returned by f() seems to persist until after second() is called. But is this guaranteed by the standard and thus other compilers? 回答1: Yes . Temporaries persist until the

shared_ptr release [duplicate]

不问归期 提交于 2019-12-11 01:15:47
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: How to release pointer from boost::shared_ptr? Detach a pointer from a shared_ptr? I'm attempting to release a shared_ptr (the way you can release a unique_ptr). I know this doesn't make sense when the shared_ptr isn't unique, but I have a shared_ptr which is guaranteed to be unique. I've tried... m_pObj.reset((T*)nullptr, [](T* const){}); ...but it just deletes the object anyway. I'm not sure what that deleter

Understanding issue with shared pointers (Lifetime, passing as parameter)

て烟熏妆下的殇ゞ 提交于 2019-12-10 23:33:09
问题 I tried to start with the boost asio chat example and derive an own networking program. Unfortunately I have some problem understanding what really happens. I tried to reduce my program to an absolute minimum. A server class waits for incoming connections and creates a session object to handle the connection. This is the code of the server: #include <cstdint> #include <iostream> #include <sstream> #include <memory> #include <vector> #include <boost/asio.hpp> #include <boost/bind.hpp> class

Shared_ptr and unique_ptr with exception

早过忘川 提交于 2019-12-10 20:37:12
问题 From en.cppreference.com Typical uses of std::unique_ptr include: providing exception safety to classes and functions that handle objects with dynamic lifetime, by guaranteeing deletion on both normal exit and exit through exception passing ownership of uniquely-owned objects with dynamic lifetime into functions acquiring ownership of uniquely-owned objects with dynamic lifetime from functions as the element type in move-aware containers, such as std::vector, which hold pointers to

Searching in a set of shared_ptr<QString>

不问归期 提交于 2019-12-10 20:14:20
问题 I have an object: class Object { public: boost::shared_ptr<QString> const& name() const {reutrn _name;} private: boost::shared_ptr<QString> _name; }; And a multi_index set typedef boost::multi_index_container< Object, boost::multi_index::indexed_by< boost::multi_index::ordered_unique< boost::multi_index::const_mem_fun< Object, boost::shared_ptr<QString> const&, & Object::name>, StringPointerLess> > > ObjectSet; Now If I want to find something in the set and I have QString I need to make a

limit on parameters taken by boost make_shared

我的未来我决定 提交于 2019-12-10 18:19:41
问题 The following piece of code compiles without an issue. In this scenario I'm sending 9 parameters to make_shared #include <boost/shared_ptr.hpp> #include <boost/make_shared.hpp> class Controller { int a1,a2,a3,a4,a5,a6,a7,a8,a9; static void createInstance(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8, int a9) { Controller::controller = boost::make_shared<Controller>(a1,a2,a3,a4,a5,a6,a7,a8,a9); } private: Controller(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int

making a vector of shared pointers from Spirit Qi

那年仲夏 提交于 2019-12-10 18:16:51
问题 This is a followup question from a previous question. I can parse into vectors of strings from my grammar, but I cannot seem to parse into a vector of shared pointers to strings ; i.e. std::vector<std::shared_ptr<std::string> > , and need a bit of help. My compiling header: #define BOOST_SPIRIT_USE_PHOENIX_V3 1 #include <boost/spirit/include/qi_core.hpp> #include <boost/spirit/include/qi.hpp> #include <boost/spirit/include/phoenix_core.hpp> #include <boost/spirit/include/phoenix_operator.hpp>

c++ problem with polymorphism and vectors of pointers

烂漫一生 提交于 2019-12-10 18:05:41
问题 Consider the following example code: class Foo { }; class Bar : public Foo { }; class FooCollection { protected: vector<shared_ptr<Foo> > d_foos; }; class BarCollection : public FooCollection { public: vector<shared_ptr<Bar> > &getBars() { // return d_foos won't do here... } }; I have a problem like this in my current project. The client code uses BarCollection , which stores pointers to Bars in d_foos which is declared in FooCollection . I'd now like to expose the collection of pointers to