shared-ptr

std::auto_ptr or boost::shared_ptr for pImpl idiom?

我的未来我决定 提交于 2019-11-28 16:52:13
问题 When using the pImpl idiom is it preferable to use a boost:shared_ptr instead of a std::auto_ptr ? I'm sure I once read that the boost version is more exception friendly? class Foo { public: Foo(); private: struct impl; std::auto_ptr<impl> impl_; }; class Foo { public: Foo(); private: struct impl; boost::shared_ptr<impl> impl_; }; [EDIT] Is it always safe to use std::auto_ptr<> or are there situations when an alternative boost smart pointer is required? 回答1: You shouldn't really use std::auto

What's your convention for typedef'ing shared_ptr?

江枫思渺然 提交于 2019-11-28 16:49:33
I'm flip-flopping between naming conventions for typedef'ing the boost::shared_ptr template. For example: typedef boost::shared_ptr<Foo> FooPtr; Before settling on a convention, I'd like to see what others use. What is your convention? EDIT: To those nesting the typedef inside Foo, doesn't it bother you that Foo is now "aware" of how it will be passed around? It seems to break encapsulation. How about this: class Foo { public: typedef std::vector<Foo> Vector }; You wouldn't do this now, would you? :-) Lena Schimmel I'd like too add some options to this old question, even though they might be

Are there any downsides with using make_shared to create a shared_ptr

萝らか妹 提交于 2019-11-28 16:29:53
Are there any downsides with using make_shared<T>() instead of using shared_ptr<T>(new T) . Boost documentation states There have been repeated requests from users for a factory function that creates an object of a given type and returns a shared_ptr to it. Besides convenience and style, such a function is also exception safe and considerably faster because it can use a single allocation for both the object and its corresponding control block, eliminating a significant portion of shared_ptr's construction overhead. This eliminates one of the major efficiency complaints about shared_ptr. I know

NULL pointer with boost::shared_ptr?

…衆ロ難τιáo~ 提交于 2019-11-28 16:29:27
What's the equivalent to the following: std::vector<Foo*> vec; vec.push_back(NULL); when dealing with boost::shared_ptr ? Is it the following code? std::vector< boost::shared_ptr<Foo> > vec; vec.push_back(boost::shared_ptr<Foo>()); Note: I may push back a lot of such objects. Should I declare a global static nullPtr object somewhere? That way only one of them would have to be constructed: boost::shared_ptr<Foo> nullPtr; Your suggestion (calling the shared_ptr<T> constructor with no argument) is correct. (Calling the constructor with the value 0 is equivalent.) I don't think that this would be

Does C++11 unique_ptr and shared_ptr able to convert to each other's type?

喜欢而已 提交于 2019-11-28 16:28:18
Does C++11 standard library provide any utility to convert from a std::shared_ptr to std::unique_ptr , or vice versa? Is this safe operation? chema989 std::unique_ptr is the C++11 way to express exclusive ownership, but one of its most attractive features is that it easily and efficiently converts to a std::shared_ptr . This is a key part of why std::unique_ptr is so well suited as a factory function return type. Factory functions can’t know whether callers will want to use exclusive ownership semantics for the object they return or whether shared ownership (i.e., std::shared_ptr ) would be

How does one downcast a std::shared_ptr?

老子叫甜甜 提交于 2019-11-28 16:10:46
问题 Consider: struct SomethingThatsABase { virtual bool IsChildOne() const { return false; } virtual bool IsChildTwo() const { return false; } }; struct ChildOne : public SomethingThatsABase { virtual bool IsChildOne() const { return true; } }; struct ChildTwo : public SomethingThatsABase { virtual bool IsChildTwo() const { return true; } }; void SomeClientExpectingAChildOne(std::shared_ptr<ChildOne> const& ptrOne) { //Does stuff } void SomeClient(std::shared_ptr<SomethingThatsABase> const& ptr)

What (not) to do in a constructor

旧时模样 提交于 2019-11-28 16:05:27
I want to ask you for your best practices regarding constructors in C++. I am not quite sure what I should do in a constructor and what not. Should I only use it for attribute initializations, calling parent constructors etc.? Or might I even put more complex functions into them like reading and parsing configuration data, setting up external libraries a.s.o. Or should I write special functions for this? Resp. init() / cleanup() ? What are the PRO's and CON's here? I figured out yet that for example I can get rid of shared pointers when using init() and cleanup() . I can create the objects on

shared_ptr vs scoped_ptr

早过忘川 提交于 2019-11-28 15:47:46
问题 scoped_ptr is not copy able and is being deleted out of the scope. So it is kind of restricted shared_ptr . So seems besides the cases when you really need to restrict the copy operation shared_ptr is better to use. Because sometimes you don’t know you need to create a copy of your object or no. So the question is: besides the cases mentioned above, could we consider that shared_ptr is better (or recommended) to use instead of scoped_ptr . Does scoped_ptr work much faster from shared_ptr , or

The cost of passing by shared_ptr

。_饼干妹妹 提交于 2019-11-28 15:31:10
I use std::tr1::shared_ptr extensively throughout my application. This includes passing objects in as function arguments. Consider the following: class Dataset {...} void f( shared_ptr< Dataset const > pds ) {...} void g( shared_ptr< Dataset const > pds ) {...} ... While passing a dataset object around via shared_ptr guarantees its existence inside f and g, the functions may be called millions of times, which causes a lot of shared_ptr objects being created and destroyed. Here's a snippet of the flat gprof profile from a recent run: Each sample counts as 0.01 seconds. % cumulative self self

boost, shared ptr Vs weak ptr? Which to use when?

你离开我真会死。 提交于 2019-11-28 15:17:59
问题 In my current project I am using boost::shared_ptr quite extensively. Recently my fellow team mates have also started using weak_ptr . I don't know which one to use and when. Apart from this, what should I do if I want to convert weak_ptr to shared_ptr . Does putting a lock on weak_ptr to create a shared_ptr affect my code in other thread? 回答1: In general and summary, Strong pointers guarantee their own validity. Use them, for example, when: You own the object being pointed at; you create it