shared-ptr

Why do shared_ptr deleters have to be CopyConstructible?

牧云@^-^@ 提交于 2019-12-03 06:19:00
问题 In C++11 std::shared_ptr has four constructors which can be passed deleter objects d of type D . The signatures of these constructors are the following: template<class Y, class D> shared_ptr(Y * p, D d); template<class Y, class D, class A> shared_ptr(Y * p, D d, A a); template <class D> shared_ptr(nullptr_t p, D d); template <class D, class A> shared_ptr(nullptr_t p, D d, A a); The standard requires in [util.smartptr.shared.const] type D to be CopyConstructible. Why is this needed? If shared

false sharing in boost::detail::spinlock_pool?

巧了我就是萌 提交于 2019-12-03 05:54:50
问题 I came across this SO question and reading it over eventually led me to look at boost::detail::spinlock_pool . The purpose of boost::detail::spinlock_pool is to reduce potential contention for a global spinlock by choosing from an array of spinlock s by hashing over the shared_ptr 's address. This seems like a reasonable solution but there seems to be a problem with the current (Boost v1.49) version's implementation. spinlock_pool manages a statically allocated array of 41 spinlock instances.

Why can't a weak_ptr be constructed from a unique_ptr?

寵の児 提交于 2019-12-03 04:42:43
问题 If I understand correctly, a weak_ptr doesn't increment the reference count of the managed object, therefore it doesn't represent ownership. It simply lets you access an object, the lifetime of which is managed by someone else. So I don't really see why a weak_ptr can't be constructed from a unique_ptr , but only a shared_ptr . Can someone briefly explain this? 回答1: std::weak_ptr can't be used unless you convert it to std::shared_ptr by the means of lock() . if the standard allowed what you

std::shared_ptr Exception Safety

对着背影说爱祢 提交于 2019-12-03 04:20:52
I just realised reading this page that the constructor of std::shared_ptr with a single pointer argument is not noexcept. Hence the following code contains a possible memory leak: std::shared_ptr<int> p3 (new int); The reasonning is that two allocations could occure: The first one before the call to the constructor The second one in the constructor of shared_ptr (This is what happens in VS 2012 for example) Two questions here: Is it true that if the second allocation throws an exception, the memory of the first one leaks ? If the answer is yes: what is the correct idiom to use std::shared_ptr?

Does shared_ptr's dtor require the use of a “deleter”?

£可爱£侵袭症+ 提交于 2019-12-03 04:10:44
It's widely known that you can use a shared_ptr to store a pointer to an incomplete type, as long as the pointer can be deleted (with well-defined behaviour) during the construction of the shared_ptr . For example, the PIMPL technique: struct interface { interface(); // out-of-line definition required ~interface() = default; // public inline member, even if implicitly defined void foo(); private: struct impl; // incomplete type std::shared_ptr<impl> pimpl; // pointer to incomplete type }; [main.cpp] int main() { interface i; i.foo(); } [interface.cpp] struct interface::impl { void foo() { std:

Using C++11 lambdas asynchronously, safely

心不动则不痛 提交于 2019-12-03 03:52:53
问题 I've come to C++11 from an Objective-C background, and one thing I'm struggling to come to terms with is the different capturing semantics of C++11 lambdas vs Objective-C "blocks". (See here for a comparison). In Objective-C, like C++, the self / this pointer is implicitly captured if you refer to a member variable. But because all objects in Objective-C are effectively "shared pointers", to use the C++ terminology, you can do this: doSomethingAsynchronously(^{ someMember_ = 42; }); ... and

shared_ptr with malloc and free

会有一股神秘感。 提交于 2019-12-03 03:44:05
问题 I have working in large application which contain c and cpp. The all files saved as cpp extension but the code is written in c- style. I mean it is define structure rather than class allocate memory through malloc and realloc and calloc.In recent They have installed boost library So I am planning to use into my existing code base So I have some following question. Can I use std::shared_ptr with malloc and free. If yes, can anyone point out me sample code base? Will it impact any functionality

Downcasting shared pointer to derived class with additional functionality - is this safe?

南笙酒味 提交于 2019-12-03 02:12:55
Consider the following outline: class Base { /* ... */ }; class Derived : public Base { public: void AdditionalFunctionality(int i){ /* ... */ } }; typedef std::shared_ptr<Base> pBase; typedef std::shared_ptr<Derived> pDerived; int main(void) { std::vector<pBase> v; v.push_back(pBase(new Derived())); pDerived p1( std::dynamic_pointer_cast<Derived>(v[0]) ); /* Copy */ pDerived p2 = std::dynamic_pointer_cast<Derived>(v[0]); /* Assignment */ p1->AdditionalFunctionality(1); p2->AdditionalFunctionality(2); /* A */ return 0; } Here I'm extending the base class with a derived class that adds

std::shared_ptr: reset() vs. assignment

此生再无相见时 提交于 2019-12-03 02:02:58
This is a basic question, but I did not find a previous post about it. The title of the following question sounds like it might be the same question as mine, but the question itself does not match the title: is it better to use shared_ptr.reset or operator =? I am confused about the purpose of the reset() member function of std::shared_ptr : what does it contribute in addition to the assignment operator? To be concrete, given the definition: auto p = std::make_shared<int>(1); Are the following two lines equivalent: p = std::make_shared<int>(5); p.reset(new int(5)); What about these: p =

Using smart pointers for class members

﹥>﹥吖頭↗ 提交于 2019-12-03 00:00:12
问题 I'm having trouble understanding the usage of smart pointers as class members in C++11. I have read a lot about smart pointers and I think I do understand how unique_ptr and shared_ptr / weak_ptr work in general. What I don't understand is the real usage. It seems like everybody recommends using unique_ptr as the way to go almost all the time. But how would I implement something like this: class Device { }; class Settings { Device *device; public: Settings(Device *device) { this->device =