shared-ptr

Implicit conversion of shared_ptr

自作多情 提交于 2019-12-12 12:04:15
问题 I have two shared_ptrs of the classes U and T where T is the base of U. It is no problem to do an implicit conversion from shared_ptr<U> to shared_ptr<T> . But is is also possible to do a conversion from shared_ptr<T> to shared_ptr<U> ? I tried the sugested solution: class T { public: virtual ~T() {} protected: void fillData() = 0; }; class Timpl : public T { public: virtual ~Timpl() {} protected: virtual void fillData() = 0; }; class U : public Timpl { public: virtual ~U() {} protected:

shared_ptr not defined when including from managed code

99封情书 提交于 2019-12-12 11:32:16
问题 I'm trying to write a managed wrapper (C++/CLI) around an unmanaged C++ static library and am having two problems: The unmanaged library uses std::shared_ptr in its headers. <memory> is included, and the header compiles as part of an unmanaged project. When I include this header in the managed project, however, I receive this error: error C2039: 'shared_ptr' : is not a member of 'std' How do I access the Values collection of a SortedDictionary<K, V> from C++/CLI? I cannot seem to find any

Custom deleters for std::shared_ptrs

为君一笑 提交于 2019-12-12 10:39:45
问题 Is it possible to use a custom deleter after creating a std::shared_ptr without using new ? My problem is that object creation is handled by a factory class and its constructors & destructors are protected, which gives a compile error, and I don't want to use new because of its drawbacks. To elaborate: I prefer to create shared pointers like this, which doesn't let you set a custom deleter (I think): auto sp1 = make_shared<Song>(L"The Beatles", L"Im Happy Just to Dance With You"); Or I can

boost::shared_ptr is it safe to use it in multiple threads?

巧了我就是萌 提交于 2019-12-12 10:37:59
问题 I was trying to find the answer for some time but I failed. Lets assume that we have a shared_ptr created from one thread. Then we pass this shared_ptr to another 2 threads (using some queue for example). So from this moment there are 2 copies of the original shared_ptr , pointing to the same raw pointer. Both owner threads will take their copies of this shared_ptr from the queue. Then they will pass it to another thread or will destroy it. Question is - is it safe? Will the raw pointer

Error while copy constructing boost::shared_ptr using C++11

风格不统一 提交于 2019-12-12 08:57:40
问题 Yesterday I installed clang 3.1 and g++ 4.7 and tried compiling a project I'm working on. I was surprised to see that it didn't compile using both of the compilers. But what surprises me most is that the problem is in boost::shared_ptr . Apparently, since that class defines a move constructor/assignment operator, the copy constructor is implicitly deleted. So this code: #include <boost/shared_ptr.hpp> int main() { boost::shared_ptr<int> x; boost::shared_ptr<int> y(x); } Does not compile.

Passing a shared pointer by reference or by value as parameter to a class

孤人 提交于 2019-12-12 08:27:29
问题 Should a shared pointer be passed by reference or by value as a parameter to a class if it is going to be copied to a member variable? The copying of the shared pointer will increment the refrence count and I don't want to make any unnecessary copies and thus ref count increments. Will passing the shared pointer as a refrence solve this? I assume it does but are there any other problems with this? Passing by value: class Boo { public: Boo() { } }; class Foo { public: Foo(std::shared_ptr<Boo>

Overhead and implementation of using shared_ptr

做~自己de王妃 提交于 2019-12-12 08:22:22
问题 Short introduction: I am working on multithread code and I have to share dynamically allocated objects between two threads. To make my code cleaner (and less error-prone) I want to explicitly "delete" objects in each thread and that's why I want to use shared_ptr . First question: I want to know if implementation of -> operator in shared_ptr has some extra overhead (e.g. larger then unique_ptr ) during run time. Objects I am talking about are usually longlife instances copied only once after

How do we return a unique_pointer member from a member function?

北城以北 提交于 2019-12-12 07:59:01
问题 I have a base class with a pointer member. I would have to make an educated guess to determine whether it should be an unique_ptr or a shared_ptr . None of them seems to solve my particular use case. class Base { public: Base(): pInt(std::unique_ptr<int>(new int(10))) {}; virtual std::unique_ptr<int> get() = 0; //Base(): pInt(std::shared_ptr<int>(new int(10))) {}; // Alternate implementation //virtual std::shared_ptr<int> get() = 0; // Alternate implementation private: std::unique_ptr<int>

was raw-pointer constructor of shared_ptr a mistake?

牧云@^-^@ 提交于 2019-12-12 07:33:32
问题 In hindsight, given make_shared , would shared_ptr have a constructor that takes a raw pointer had it been introduced with C++11? Are there strong arguments or use cases in favor of this constructor? It would have avoided the well documented pitfall of exception-safety and the memory allocation/performance advantage of using make_shared . I believe another advantage of requiring shared_ptr construction via make_shared would be that it could be a single pointer under the hood, lowering its

questions regarding shared_from_this

北城以北 提交于 2019-12-12 07:10:03
问题 I have a function which takes a shared_ptr<MyClass> . In some member function memfun of MyClass , I need to pass this to that function. But if I write void MyClass:memfun() { func(shared_ptr<MyClass>(this)) } I am assuming that after the call has ended the reference count will reach 0 and this will be attempted to be destroyed, which is bad. Then I remembered that there this class enable_shared_from_this with the function shared_from_this . So now I am going to use the following: class