shared-ptr

Is there such thing as a weak_ptr that can't be locked (promoted to shared_ptr)? If not, why?

笑着哭i 提交于 2019-12-08 20:01:31
Maybe this question has been asked before, but I've never found a satisfactory answer. Also, for the purposes of simplicity assume I'm talking about a single-threaded application. So, what I've heard a number of times is that if you have an object that is non-owned and whose lifetime is guaranteed , you should reference it with a raw pointer. The object's owner would use a unique_ptr, and hand out raw pointers as necessary. But what if the object is non-owned , and the lifetime is not guaranteed ? Then you can use a weak_ptr, yes. But then anyone who is handed a weak_ptr could be naughty and

Is there a weak_ptr equivalent to shared_from_this?

江枫思渺然 提交于 2019-12-08 19:35:56
问题 I have a class which I know will always be owned by a std::shared_ptr . However passing shared_ptr or even weak_ptr to functions and methods that don't need ownership or lifetime guarantees creates unnecessary overhead. To get around this I often pass raw pointers to functions. The class itself inherits from std::enable_shared_from_this so if the function needs to take ownership of the pointer it can use a method of the class to get a shared_ptr . This is all working beautifully. However

Have I misunderstood the scope of this default argument shared_ptr?

北慕城南 提交于 2019-12-08 19:25:25
问题 Take a look at this: #include <iostream> #include <memory> using Foo = int; using FooPtr = std::shared_ptr<Foo>; FooPtr makeFoo() { FooPtr f{ new Foo(), [](Foo* ptr) { delete ptr; std::cerr << "!\n"; } }; return f; } void bar(FooPtr p = {}) { p = makeFoo(); } int main() { bar(); } // Expected output: '!' // Failure case: no output (deleter not invoked?) I expected the shared_ptr deleter to be called when bar() returns, and on my 64-bit CentOS 7 system using GCC 4.8.5, it does. However, on my

Is the state of any standard class after being moved specified?

我与影子孤独终老i 提交于 2019-12-08 17:08:02
问题 If I move shared_ptr 'a' into shared_ptr 'b' is 'a' guaranteed to be null? Is the state of any standard class after being moved specified? 回答1: In general 17.6.5.15/1 applies: Objects of types defined in the C++ standard library may be moved from (12.8). Move operations may be explicitly specified or implicitly generated. Unless otherwise specified, such moved-from objects shall be placed in a valid but unspecified state. Thus you can call any functions which requires no precondition. If

atomic operations for shared_ptr in C++11

限于喜欢 提交于 2019-12-08 15:19:10
问题 By reading the c++11 draft n3242, section 20.7.2.5, looks like we have atomic operations on shared_ptr, which enables us do lock-free on complicated structure without worrying about GC/memory leak. However, I couldn't use it successfully in GCC-4.7.0. I simply tested the following program #include <atomic> #include <memory> #include <string> struct X { int x; double y; std::string s; }; int main() { std::shared_ptr<X> x(new X); auto p = std::atomic_load(&x); } and it has compiler error: c.cpp

When is object pointed by std::shared_ptr deleted?

人盡茶涼 提交于 2019-12-08 11:58:34
问题 In my library, I use std:shared_ptr`s to hold communication objects, I am working with. I have template function creating those pointers. It returns raw pointers so application could use those objects, without overhead with reference counting (strict realtime application). template<typename COBTYPE> inline COBTYPE* CLink::createCob(COBTYPE cob) { std::shared_ptr<CCob> c(cob); if(c->init(this)!=OK){ trace(ERROR,"Cannot add cob, failed to initialize\n"); return NULL; } mCobs.push_back(c); /

Get a shared pointer from a dereferenced value of a shared pointer

本秂侑毒 提交于 2019-12-08 11:06:45
问题 Imagine having a vector of shared pointers typedef vector< shared_ptr< classA > > PointerVector; And a Class B which has as a member a vector of shared pointers as well, and a method that pushes back in this vector an already dereferenced shared Pointer. ClassB { public: void add(classA &ref); private: PointerVector vector; } Assume that main feeds the add function with a dereferenced shared_ptr< classA > instance: in main: shared_ptr< classA > sharedptr ( new classA ); ClassA &ref =

Allocating class member with std::shared_ptr

走远了吗. 提交于 2019-12-08 05:30:18
问题 Is my assumption, that in following example, memory referenced by b will be deallocated once instance of A goes out of scope at end of func() , correct? class A{ public: A() { b = std::shared_ptr<char>(new char[100] { 0 } ); } char* b; } void func { A a; } 回答1: No, not correct. b is of type char * and you assign to it a shared_ptr<char> . You should get a compilation error. Furthermore, the constructor is private , another compilation error. And how do you access b in func() ? It is private

accessing operator overloading of class which is wrapped by std::shared_ptr

孤者浪人 提交于 2019-12-08 03:36:57
问题 the idea is that I want a class which is wrapped by std::shared_ptr , can still be used just like they weren't a pointer, for example the operator= which was defined in my class can still be used after my class is wrapped by std::shared_ptr . for example template <class Ty> class shared_ptr_proxy : public std::shared_ptr<Ty> { public: template<class Other> shared_ptr_proxy& operator=(const Other& rhs) { (*this->get()) = rhs; return *this; } template<class Other> explicit shared_ptr_proxy

Pass C++ object contained in a smart pointer to Python

元气小坏坏 提交于 2019-12-08 02:45:14
问题 I have a class in C++. I create an object from this class in my C++ code. I want this object to be accessible in Python. I use boost::shared_ptr to keep the object address. I've checked out some posts about this but wasn't very helpful. I think the best way is to make an object in Python namespace after interpreter initialization and then assign my boost shared_ptr to the created object in Python. I've wrapped my class using BOOST_PYTHON_MODULE in cpp and tested some ways like namespace["my