shared-ptr

shared_ptr aliasing constructor

为君一笑 提交于 2019-11-29 18:36:03
问题 Question about following shared_ptr constructor: template< class Y > shared_ptr( const shared_ptr<Y>& r, T *ptr ); Am I correct that if r was created using user-provided deleter, then aliasing shared_ptr knows that. So if aliasing shared_ptr is last in the group and (when going out of scope) destructs resources originally managed by r , it will use that user-provided deleter? 回答1: Example: #include <iostream> #include <iomanip> struct some_type { int i; }; void my_deleter(some_type* p) { std:

Double inheritance of enable_shared_from_this

蹲街弑〆低调 提交于 2019-11-29 18:19:43
问题 I have an object (Z) which derives from two other objects (A and B). A and B both derive from enable_shared_from_this<> , respectively enable_shared_from_this<A> and enable_shared_from_this<B> . Of course I call shared_from_this() on Z. And of course the compiler reports this as ambiguous. My questions are : is it safe to inherit twice from enable_shared_from_this<> or will it create two separated reference counts (bad !) If not safe, how do I solve this ? Note : I've found this other

Extracting a raw pointer from a shared_ptr

混江龙づ霸主 提交于 2019-11-29 17:56:23
Is it possible to extract a raw pointer from a std::shared_ptr or std::tr1::shared_ptr object? The intent is to tell the smart pointer object that I don't want it to manage the lifetime of the object anymore. The context is that I have an API that takes a raw pointer from users and does some processing on the object. To make things easier to manage API creates a shared_ptr out of this raw pointer. Now, the user might ask for the object to be returned. In that case, when giving the processed object back to the user I want to give back the raw pointer. However, I have not found a way to do that.

Boost Python Runtime error when passing object of derived type from python to C++ function expecting a shared_ptr to base type

混江龙づ霸主 提交于 2019-11-29 16:36:31
I have a function that takes a std::shared_ptr, and I want to pass an object of Derived type to this function from python. Here's my class definitions: struct AbstractBase { virtual void foo() = 0; }; struct Derived : public AbstractBase { virtual void foo(){ std::cout<<"Derived's foo!"<<std::endl; } }; struct Unrelated { void bar(std::shared_ptr<AbstractBase> base_shared_ptr) { base_shared_ptr->foo(); } }; #endif /* CLASSES_H */ A simple pure C++ example does what I want: int main() { std::shared_ptr<Derived> d(new Derived); Unrelated u; u.bar(d); } output: Derived's foo! Here is my Boost

Can you allocate an array with something equivalent to make_shared?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-29 16:22:48
问题 buffer = new char[64]; buffer = std::make_shared<char>(char[64]); ??? Can you allocate memory to an array using make_shared<>() ? I could do: buffer = std::make_shared<char>( new char[64] ); But that still involves calling new, it's to my understanding make_shared is safer and more efficient. 回答1: The point of make_shared is to incorporate the managed object into the control block of the shared pointer, Since you're dealing with C++11, perhaps using a C++11 array would satisfy your goals?

How do you make std::shared_ptr not call delete()

安稳与你 提交于 2019-11-29 16:04:43
问题 I have functions that take in std::shared_ptr as an argument so I am forced to use std::shared_ptr, but the object I am passing to the function is not dynamically allocated. How do I wrap the object in std::shared_ptr and have std::shared_ptr not call delete on it. 回答1: MyType t; nasty_function(std::shared_ptr<MyType>(&t, [](MyType*){})); 回答2: Specify a no-op deleter when creating the shared pointer. E.g. like this: void null_deleter(MyType *) {} int main() { MyType t; nasty_function(std:

How can I call a private destructor from a shared_ptr?

拜拜、爱过 提交于 2019-11-29 14:20:53
问题 I have a resource_manager class which maintains a std::vector<boost::shared_ptr<resource> > internally. resource_manager is a friend class of resource . I want resource s to only be created/deleted by resource_manager , so I made its constructors private (which works ok). However, if I make the destructor private, the code doesn't compile because the destructor is called by boost::shared_ptr , which is not a friend of resource . I am thinking of enforcing the "do not delete by clients" rule

Errors in std::make_shared() when trying to make shared_ptr?

江枫思渺然 提交于 2019-11-29 14:14:49
(Using Visual Studio 2010) I'm trying to create a shared_ptr of an existing class in my project (class was written a decade before std::shared_ptr existed). This class takes a non-const pointer to another object, it's empty parameter constructor is private. class Foobar { public: Foobar(Baz* rBaz); private: Foobar(); } When I try to create a shared_ptr to it, things don't go well: Baz* myBaz = new Baz(); std::shared_ptr<Foobar> sharedFoo = std::make_shared<Foobar>(new Foobar(myBaz)); On VS2010, this gives me error C2664: 'Foobar::Foobar(const Foobar &)' : cannot convert parameter 1 from

std::enable_shared_from_this; public vs private

末鹿安然 提交于 2019-11-29 13:28:49
I was playing around for a bit using the shared_ptr's and enable_shared_from_this, while I run into something I don't really understand. In my first attempt I constructed something like this: class shared_test : std::enable_shared_from_this<shared_test> { public: void print(bool recursive) { if (recursive) { shared_from_this()->print(false); } std::cout << "printing" << std::endl; } }; Please note that this class is extending std::enable_shared_from_this privately. This apparently makes a lot of difference because executing a something like this: int main() { auto t(std::make_shared<shared

boost::weak_ptr<T>.lock() Crashes with a SIGSEGV Segmentation Fault

 ̄綄美尐妖づ 提交于 2019-11-29 12:48:43
(EDIT) Environment: plee@sos-build:/usr/local/include/boost$ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 11.10 Release: 11.10 Codename: oneiric plee@sos-build:/usr/local/include/boost$ uname -a Linux sos-build 3.0.0-12-generic #20-Ubuntu SMP Fri Oct 7 14:56:25 UTC 2011 x86_64 x86_64 x86_64 GNU/Linux plee@sos-build:/usr/local/include/boost$ plee@sos-build:/usr/local/include/boost$ cat version.hpp // BOOST_LIB_VERSION must be defined to be the same as BOOST_VERSION #define BOOST_LIB_VERSION "1_47" I have been working on a server-side project. I use