shared-ptr

Should I switch from using boost::shared_ptr to std::shared_ptr?

丶灬走出姿态 提交于 2019-11-30 10:17:03
问题 I would like to enable support for C++0x in GCC with -std=c++0x . I don't absolutely necessarily need any of the currently supported C++11 features in GCC 4.5 (and soon 4.6), but I would like to start getting used to them. For example, in a few places where I use iterators, an auto type would be useful. But again, I don't need any of the currently supported features. The goal here is to encourage me to incorporate the features of the new standard into my programming "vocabulary". From what

bad weak pointer when base and derived class both inherit from boost::enable_shared_from_this

谁说我不能喝 提交于 2019-11-30 10:06:59
I have a base class which derives from boost::enable_shared_from_this, and then another class which derives from both the base class and boost::enable_shared_from_this: #include <boost/enable_shared_from_this.hpp> #include <boost/shared_ptr.hpp> using namespace boost; class A : public enable_shared_from_this<A> { }; class B : public A , public enable_shared_from_this<B> { public: using enable_shared_from_this<B>::shared_from_this; }; int main() { shared_ptr<B> b = shared_ptr<B>(new B()); shared_ptr<B> b_ = b->shared_from_this(); return 0; } This compiles but at runtime it gives terminate

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

独自空忆成欢 提交于 2019-11-30 09:49:17
问题 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:

How can I call a private destructor from a shared_ptr?

醉酒当歌 提交于 2019-11-30 09:30:01
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 by only returning only const resource* from the resource_manager , but somehow I am not satisfied with

Does enable_shared_from_this and make_shared provide the same optimization

心不动则不痛 提交于 2019-11-30 09:00:05
As I understand make_shared<T>(...) may provide some memory allocation optimization (it may allocate reference counter within same memory block as instance of class T). Do enable_shared_from_this provides the same optimization? So: class T : std::enable_shared_from_this<T> {}; ... auto t = std::shared_ptr<T>(new T); Is the same as: class T {}; ... auto t = std::make_shared<T>(); If not take in account sizeof(T). Do enable_shared_from_this provides the same optimization? So: No. As you can see from the wording in the standard, enable_shared_from_this<T> has a weak_ptr<T> data member. That adds

How to accomplish covariant return types when returning a shared_ptr?

喜夏-厌秋 提交于 2019-11-30 08:13:18
using namespace boost; class A {}; class B : public A {}; class X { virtual shared_ptr<A> foo(); }; class Y : public X { virtual shared_ptr<B> foo(); }; The return types aren't covariant (nor are they, therefore, legal), but they would be if I was using raw pointers instead. What's the commonly accepted idiom to work around this, if there is one? I think that a solution is fundamentally impossible because covariance depends on pointer arithmetic which is incompatible with smart pointers. When Y::foo returns shared_ptr<B> to a dynamic caller, it must be cast to shared_ptr<A> before use. In your

Usage of std::shared_ptr

可紊 提交于 2019-11-30 05:42:29
问题 How can I use std::shared_ptr for array of double? Additionally what are advantages/disadvantages of using shared_ptr. 回答1: It depends on what you're after. If you just want a resizable array of doubles, go with std::vector<double> Example: std::vector<double> v; v.push_back(23.0); std::cout << v[0]; If sharing the ownership of said array matters to you, use e.g. std::shared_ptr<std::vector<double>> Example: std::shared_ptr<std::vector<double>> v1(new std::vector<double>); v1->push_back(23.0)

C++ shared_ptr vs. unique_ptr for resource management

亡梦爱人 提交于 2019-11-30 05:03:05
问题 I've been mulling over use of unique_ptr vs shared_ptr vs own_solution . I've discounted the latter as I'll almost certainly get it wrong, but I have a problem with both unique_ptr and shared_ptr in that neither captures precisely what I want. I want to create a resource manager which explicitly owns a resource, however I'd like the resource manager to also hand out references to the resource. If I use unique_ptr in the resource manager and hand out raw pointers there's the possibility they

Smart pointer wrapping penalty. Memoization with std::map

一笑奈何 提交于 2019-11-30 04:56:39
问题 I am currently in the middle of a project where performance is of vital importance. Following are some of the questions I had regarding this issue. Question1 My project involves plenty of boost::shared_ptr .I know creating shared pointers on the run using boost::make_shared is slow since there is a lot of overhead as it needs to track references.I wanted to know what if the boost shared pointers are already created then would these two statements have the same performance or would one be

getting a normal ptr from shared_ptr?

我们两清 提交于 2019-11-30 04:11:28
I have something like shared_ptr<Type> t(makeSomething(), mem_fun(&Type::deleteMe)) I now need to call C styled function that requires a pointer to Type . How do I get it from shared_ptr ? Use the get() method: boost::shared_ptr<foo> foo_ptr(new foo()); foo *raw_foo = foo_ptr.get(); c_library_function(raw_foo); Make sure that your shared_ptr doesn't go out of scope before the library function is done with it -- otherwise badness could result, since the library may try to do something with the pointer after it's been deleted. Be especially careful if the library function maintains a copy of the