shared-ptr

Why does unique_ptr take two template parameters when shared_ptr only takes one?

痴心易碎 提交于 2019-11-27 18:05:11
Both unique_ptr and shared_ptr accept a custom destructor to call on the object they own. But in the case of unique_ptr , the destructor is passed as a template parameter of the class , whereas the type of shared_ptr 's custom destructor is to be specified as a template parameter of the constructor . template <class T, class D = default_delete<T>> class unique_ptr { unique_ptr(T*, D&); //simplified ... }; and template<class T> class shared_ptr { template<typename D> shared_ptr(T*, D); //simplified ... }; I can't see why such difference. What requires that? If you provide the deleter as

Conversion from boost::shared_ptr to std::shared_ptr?

拟墨画扇 提交于 2019-11-27 17:59:10
I got a library that internally uses Boost's version of shared_ptr and exposes only those. For my application, I'd like to use std::shared_ptr whenever possible though. Sadly, there is no direct conversion between the two types, as the ref counting stuff is implementation dependent. Is there any way to have both a boost::shared_ptr and a std::shared_ptr share the same ref-count-object? Or at least steal the ref-count from the Boost version and only let the stdlib version take care of it? You can carry the boost::shared_ptr "inside" the std::shared_ptr by using the destructor to carry the

Is make_shared really more efficient than new?

倖福魔咒の 提交于 2019-11-27 17:41:34
I was experimenting with shared_ptr and make_shared from C++11 and programmed a little toy example to see what is actually happening when calling make_shared . As infrastructure I was using llvm/clang 3.0 along with the llvm std c++ library within XCode4. class Object { public: Object(const string& str) { cout << "Constructor " << str << endl; } Object() { cout << "Default constructor" << endl; } ~Object() { cout << "Destructor" << endl; } Object(const Object& rhs) { cout << "Copy constructor..." << endl; } }; void make_shared_example() { cout << "Create smart_ptr using make_shared..." << endl

Garbage collection vs. shared pointers

↘锁芯ラ 提交于 2019-11-27 16:04:29
问题 What are the differences between shared pointers (such as boost::shared_ptr or the new std::shared_ptr) and garbage collection methods (such as those implemented in Java or C#)? The way I understand it, shared pointers keep track of how many times variables points to the resource and will automatically destruct the resource when the count reaches zero. However, my understanding is that the garbage collector also manages memory resources, but requires additional resources to determine if an

std::shared_ptr and initializer lists

拜拜、爱过 提交于 2019-11-27 15:52:05
问题 The std::shared_ptr constructor isn't behaving as I expected: #include <iostream> #include <vector> void func(std::vector<std::string> strings) { for (auto const& string : strings) { std::cout << string << '\n'; } } struct Func { Func(std::vector<std::string> strings) { for (auto& string : strings) { std::cout << string << '\n'; } } }; int main(int argc, const char * argv[]) { func({"foo", "bar", "baz"}); Func({"foo", "bar", "baz"}); //auto ptr = std::make_shared<Func>({"foo", "bar", "baz"});

weak_ptr, make_shared and memory deallocation

巧了我就是萌 提交于 2019-11-27 15:48:59
问题 A control block of a shared_ptr is kept alive while there is at least one weak_ptr present. If the shared pointer was created with make_shared that implies that the whole memory of the object is kept allocated. (The object itself is properly destructed, but since the control block and the memory for the object were allocated in one chunk, as make_shared does, they can only be deallocated together.) Is my understanding correct? It seems that this behaviour represents a problem, for example in

Why is GoogleMock leaking my shared_ptr?

喜欢而已 提交于 2019-11-27 15:36:50
问题 I use GoogleMock/GoogleTest for testing, and I'm seeing some strange behavior when a matcher has a shared_ptr to a mock as a parameter, and EXPECT is called on the same shared_ptr. The offending piece of code: #include <gmock/gmock.h> #include <gtest/gtest.h> #include <boost/shared_ptr.hpp> #include <boost/make_shared.hpp> using namespace boost; using namespace testing; struct MyParameter { virtual ~MyParameter() {} virtual void myMethod() = 0; }; struct MyParameterMock : public MyParameter {

shared_ptr and cyclic references

匆匆过客 提交于 2019-11-27 15:03:24
问题 I was trying with the cyclic references for boost::shared_ptr , and devised following sample: class A{ // Trivial class public: i32 i; A(){} A(i32 a):i(a){} ~A(){ cout<<"~A : "<<i<<endl; } }; shared_ptr<A> changeI(shared_ptr<A> s){ s->i++; cout<<s.use_count()<<'\n'; return s; } int main() { shared_ptr<A> p1 = make_shared<A>(3); shared_ptr<A> p2 = p1; shared_ptr<A> p3 = p2; shared_ptr<A> p4 = p3; p1 = p4; // 1) 1st cyclic ref. cout<<p1.use_count()<<'\n'; p1 = changeI(p4); // 2) 2nd cyclic ref.

Detach a pointer from a shared_ptr? [duplicate]

人走茶凉 提交于 2019-11-27 14:18:08
Possible Duplicate: How to release pointer from boost::shared_ptr? A function of my interface returns a pointer to an object. The user is supposed to take ownership of that object. I do not want to return a Boost.shared_ptr, because I do not want to force clients to use boost. Internally however, I would like to store the pointer in a shared_ptr to prevent memory leaks in case of exceptions etc. There seems to be no way to detach a pointer from a shared pointer. Any ideas here? James McNellis What you're looking for is a release function; shared_ptr doesn't have a release function. Per the

Deleter type in unique_ptr vs. shared_ptr [duplicate]

夙愿已清 提交于 2019-11-27 13:39:21
问题 This question already has answers here : Why does unique_ptr take two template parameters when shared_ptr only takes one? (2 answers) Closed 4 years ago . I thought it is very curious when I discovered that the standard defines std::unique_ptr and std::shared_ptr in two totally different ways regarding a Deleter that the pointer may own. Here is the declaration from cppreference::unique_ptr and cppreference::shared_ptr: template< class T, class Deleter = std::default_delete<T> > class unique