shared-ptr

Customising std::shared_ptr or boost::shared_ptr to throw an exception on NULL dereference

こ雲淡風輕ζ 提交于 2019-12-05 06:59:00
I have a few projects that use boost::shared_ptr or std::shared_ptr extensively (I can convert to either implementation soon enough, if there is a good answer to this question for one, but not the other). The Boost implementation uses Boost.Assert to avoid returning in the case of encountering an empty (NULL) pointer in operator* or operator-> at runtime; while the libc++ implementation seems to lack any check. While of course the validity of a shared_ptr should be checked before use, a large, mixed-paradigm codebase leads me to want to try an exception-throwing variation; as most of the code

specialise `std::default_delete` for `std::shared_ptr`

我的未来我决定 提交于 2019-12-05 06:20:27
I have the idea to do this: namespace std { template<> class default_delete<IplImage> { public: void operator()(IplImage *ptr) const { cvReleaseImage(&ptr); } }; }; typedef std::shared_ptr<IplImage> IplImageObj; I didn't really found much information whether it is supported that I specialise default_delete and whether shared_ptr also uses default_delete by default. It works like intended with Clang 5.0.0. So, is it supported? What if the STL implementation has a different internal namespace? It wouldn't find my declaration then? But it should error about the declaration then. ForEveR default

C++11 make_shared instancing

时光怂恿深爱的人放手 提交于 2019-12-05 05:48:59
Apologies for the long question, but some context is necessary. I have a bit of code that seems to be a useful pattern for the project I'm working on: class Foo { public: Foo( int bar = 1 ); ~Foo(); typedef std::shared_ptr< Foo > pointer_type; static pointer_type make( int bar = 1 ) { return std::make_shared< Foo >( bar ); } ... } As you can see, it provides a straightforward way of constructing any class as a PointerType which encapsulates a shared_ptr to that type: auto oneFoo = Foo::make( 2 ); And therefore you get the advantages of shared_ptr without putting references to make_shared and

Are weak pointers guaranteed to have expired by the time the std::shared_ptr deleter runs?

一个人想着一个人 提交于 2019-12-05 05:43:22
If I have a std::shared_ptr<Foo> with a custom deleter, is it guaranteed that all associated weak pointers are seen as expired by the deleter? (I would appreciate it very much if you could cite relevant sections in the standard.) In other words is the assertion below guaranteed not to fire? std::weak_ptr<Foo> weak; std::shared_ptr<Foo> strong{ new Foo, [&weak] (Foo* f) { assert(weak.expired()); delete f; }, }; weak = strong; strong.reset(); The standard guarantees nothing. For shared_ptr 's destructor, the spec only says: If *this is empty or shares ownership with another shared_ptr instance (

Wrapping std::vector of boost::shared_ptr in SWIG for Python

谁说胖子不能爱 提交于 2019-12-05 05:27:59
问题 EDIT: Solved, my mistake; explained in my answer. I have this: std::vector < boost::shared_ptr < Entity > > entities; and I try to expose it through SWIG like this: %include "boost_shared_ptr.i" %include "std_vector.i" %shared_ptr(Entity) %include <Entity.h> namespace std { %template(EntityVector) vector<boost::shared_ptr<Entity> >; }; %include <TheFileWithEntities.h> However, in Python entities ends up being a tuple: import MyModule print type(MyModule.cvar.entities) # Output: (type 'tuple')

intrusive_ptr: Why isn't a common base class provided?

天涯浪子 提交于 2019-12-05 03:01:00
boost::intrusive_ptr requires intrusive_ptr_add_ref and intrusive_ptr_release to be defined. Why isn't a base class provided which will do this? There is an example here: http://lists.boost.org/Archives/boost/2004/06/66957.php , but the poster says "I don't necessarily think this is a good idea". Why not? Update: I don't think the fact that this class could be misused with Multiple Inheritance is reason enough. Any class which derives from multiple base classes with their own reference count would have the same issue. Whether these refcounts are implemented via a base class or not makes no

Call default copy constructor from within overloaded copy constructor

时光总嘲笑我的痴心妄想 提交于 2019-12-05 01:10:20
I need to write a copy constructor that deep copies the contents of a std::shared_ptr . However, there are a bunch of variable int a, b, c, d, e; also defined in the class. Is there a way to generate the default copy constructor code (or call the default copy constructor) inside my new overloaded one. Here is a code snippet with a comment that hopefully clarifies the issue. class Foo { public: Foo() {} Foo(Foo const & other); ... private: int a, b, c, d, e; std::shared_ptr<Bla> p; }; Foo::Foo(Foo const & other) { p.reset(new Bla(*other.p)); // Can I avoid having to write the default copy

Does make_shared do a default initialization (zero-init) for each member variable

心不动则不痛 提交于 2019-12-05 01:06:51
Take an ordinary struct (or class) with Plain Old Data types and objects as members. Note that there is no default constructor defined. struct Foo { int x; int y; double z; string str; }; Now if I declare an instance f on the stack and attempt to print its contents: { Foo f; std::cout << f.x << " " << f.y << " " << f.z << f.str << std::endl; } The result is garbage data printed for x, y, and z. And the string is default initialized to be empty. As expected. If I create an instance of a shared_ptr<Foo> using make_shared and print: { shared_ptr<Foo> spFoo = make_shared<Foo>(); cout << spFoo->x <

testing if a shared_ptr is NULL

醉酒当歌 提交于 2019-12-05 00:54:33
I have the following code snippet: std::vector< boost::shared_ptr<Foo> >::iterator it; it = returnsAnIterator(); // often, it will point to a shared_ptr that is NULL, and I want to test for that if(*it) { // do stuff } else // do other stuff Am I testing correctly? The boost docs say that a shared_ptr can be implicitly converted to a bool, but when I run this code it segfaults: Program received signal SIGSEGV, Segmentation fault. 0x0806c252 in boost::shared_ptr<Foo>::operator Foo* boost::shared_ptr<Foo>::* (this=0x0) at /usr/local/bin/boost_1_43_0/boost/smart_ptr/detail/operator_bool.hpp:47 47

What is the need for enable_shared_from_this? [duplicate]

試著忘記壹切 提交于 2019-12-05 00:18:13
问题 This question already has answers here : What is the usefulness of `enable_shared_from_this`? (6 answers) Closed 3 years ago . I am new to C++11 and I came across enable_shared_from_this. I do not understand what it is trying to achieve? So I have a program that uses enable_shared_from_this. struct TestCase: enable_shared_from_this<TestCase> { std::shared_ptr<testcase> getptr() { return shared_from_this(); } ~TestCase() { std::cout << "TestCase::~TestCase()"; } }; int main() { std::shared_ptr