shared-ptr

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

只谈情不闲聊 提交于 2019-12-22 05:34:09
问题 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?

Same address, multiple shared_ptr counters, is it forbidden by C++ standard?

送分小仙女□ 提交于 2019-12-22 05:14:36
问题 Suppose I have the need to do the following (This is just some imaginative code for discussion of the C++ standard, thus I won't discuss why I design it this way, so don't bother me with something like: your design is wrong.) T* ptr = new T; shared_ptr<T> p(ptr); shared_ptr<T> q(ptr, SomeDeleterThatDoesnotDeleteButDoSomeOtherStuff()); Suppose the logic guarantees that p or some of its copies lives longer than all copies of q , so practically there won't be any problem. My question is, is it

QSharedPointer and QObject::deleteLater

旧时模样 提交于 2019-12-22 04:13:07
问题 I have a situation where a QSharedPointer managed object signalizes that it has finished it's purpose and is ready for deletion soon (after execution left the function emitting my readyForDeletion signal). When working with normal pointers, I'd just call QObject::deleteLater on the object, however this isn't possible with a QSharedPointer -managed instance. My workaround is the following: template<typename T> class QSharedPointerContainer : public QObject { QSharedPointer<T> m_pSharedObj;

QSharedPointer and QObject::deleteLater

◇◆丶佛笑我妖孽 提交于 2019-12-22 04:13:04
问题 I have a situation where a QSharedPointer managed object signalizes that it has finished it's purpose and is ready for deletion soon (after execution left the function emitting my readyForDeletion signal). When working with normal pointers, I'd just call QObject::deleteLater on the object, however this isn't possible with a QSharedPointer -managed instance. My workaround is the following: template<typename T> class QSharedPointerContainer : public QObject { QSharedPointer<T> m_pSharedObj;

Verify at compile time that objects are created as shared_ptr

筅森魡賤 提交于 2019-12-21 13:42:20
问题 There are classes that I write (often as part of boost::asio ) whose objects depend on being wrapped in a shared_ptr because they use shared_from_this() . Is there a way to prevent an object from being compiled if it's not instantiated in a shared_ptr ? So, what I'm looking for: std::shared_ptr<MyClass> a = std::make_shared<MyClass>(); // should compile fine std::unique_ptr<MyClass> a = std::make_unique<MyClass>(); // compile error MyClass a; // compile error 回答1: Make its constructor private

Shared pointers delete recursive data structures recursively and the stack overflows

杀马特。学长 韩版系。学妹 提交于 2019-12-21 10:25:30
问题 I have a number of long linked lists (they have up to 20,000 items). They have different beginnings but they can eventually point to the same node from some node onwards. I've decided to let such linked list to grow together and share the memory between them. That is why I ve decided to implement linked list with shared pointers: #include <memory> struct SharedLinkedList { int someData; std::shared_ptr<SharedLinkedList> next; }; This way everything works fine. The linked lists which are no

why allocate_shared and make_shared so slow

我们两清 提交于 2019-12-21 09:13:35
问题 I just wrote a test program to find the fastest way to allocate & free many objects which managed by shared_ptr . I tried shared_ptr with new , shared_ptr with pool , make_shared , allocate_shared . What make me surprised is allocate_shared is slower than shared_ptr with pool . I test the code in vs2017+win10 with release build. The release build setting is default(/O2). I also test it in gcc4.8.5+centos6.2 with g++ -std=c++11 -O3 . The code is: #include <memory> #include <iostream> #include

What does “single allocation” mean for boost::make_shared

回眸只為那壹抹淺笑 提交于 2019-12-21 09:09:09
问题 In the boost doc of make_shared, it says: 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. I don't understand the meaning of "single allocation", what does it mean? 回答1: An "allocation" means a block of memory obtained from a call to an allocator. Usually, creating a shared_ptr

What does “single allocation” mean for boost::make_shared

拜拜、爱过 提交于 2019-12-21 09:08:29
问题 In the boost doc of make_shared, it says: 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. I don't understand the meaning of "single allocation", what does it mean? 回答1: An "allocation" means a block of memory obtained from a call to an allocator. Usually, creating a shared_ptr

shared_ptr in std::tr1

柔情痞子 提交于 2019-12-21 09:04:04
问题 I am working on a platform with a gcc compiler however boost cannot compile on it. I am wondering what is the proper way to include the shared_ptr in std:tr1 on gcc? the file i looked in said not to include it directly, from what i can tell no other file includes it either :| 回答1: In G++ 4.3 , #include <tr1/memory> should do the trick. You'll find shared_ptr at std::tr1::shared_ptr . 回答2: Boost itself has the answer. 回答3: Boost can not compile on it? Most of the boost library doesn't need to