smart-pointers

Why is shared_ptr<void> legal, while unique_ptr<void> is ill-formed?

邮差的信 提交于 2019-11-27 10:12:37
问题 The question really fits in the title: I am curious to know what is the technical reason for this difference, but also the rationale ? std::shared_ptr<void> sharedToVoid; // legal; std::unique_ptr<void> uniqueToVoid; // ill-formed; 回答1: It is because std::shared_ptr implements type-erasure, while std::unique_ptr does not. Since std::shared_ptr implements type-erasure, it also supports another interesting property, viz. it does not need the type of the deleter as template type argument to the

Passing unique_ptr to functions

血红的双手。 提交于 2019-11-27 10:07:01
问题 I'm trying to "modernize" some existing code. I have a class which currently has a member variable "Device* device_". It uses new to create an instance in some initialization code and has a "delete device_" in the destructory. Member functions of this class call many other functions that take a Device* as a parameter. This works well, but to "modernize" my code I thought I ought to change the variable to be defined as "std::unique_ptr<Device> device_" and remove the explicit call to delete,

Does C++11 have wrappers for dynamically-allocated arrays like Boost's scoped_array?

寵の児 提交于 2019-11-27 10:00:32
问题 I often need to deal with dynamically-allocated arrays in C++, and hence rely on Boost for scoped_array, shared_array, and the like. After reading through Stroustrup's C++11 FAQ and the C++11 Reference Wiki, I could not find a suitable replacement for these dynamic array wrappers that is provided by the C++11 standard. Is there something that I have overlooked, or do I have to continue relying on Boost? Thank you very much for your help! 回答1: There is a specialization of unique_ptr , like

unique_ptr and OpenSSL's STACK_OF(X509)*

为君一笑 提交于 2019-11-27 09:31:53
I use some using statements and unique_ptr to work with OpenSSL, as suggested in another question . Without, code becomes really ugly and I am not so much a fan of goto statements. So far I have changed my code as far as possible. Here are examples, what I use: using BIO_ptr = std::unique_ptr<BIO, decltype(&::BIO_free)>; using X509_ptr = std::unique_ptr<X509, decltype(&::X509_free)>; using EVP_PKEY_ptr = std::unique_ptr<EVP_PKEY, decltype(&::EVP_PKEY_free)>; using PKCS7_ptr = std::unique_ptr<PKCS7, decltype(&::PKCS7_free)>; ... BIO_ptr tbio(BIO_new_file(some_filename, "r"), ::BIO_free); Now I

How to avoid memory leak with shared_ptr?

大兔子大兔子 提交于 2019-11-27 07:17:21
Consider the following code. using boost::shared_ptr; struct B; struct A{ ~A() { std::cout << "~A" << std::endl; } shared_ptr<B> b; }; struct B { ~B() { std::cout << "~B" << std::endl; } shared_ptr<A> a; }; int main() { shared_ptr<A> a (new A); shared_ptr<B> b (new B); a->b = b; b->a = a; return 0; } There is no output . No desctructor is called. Memory leak. I have always believed that the smart pointer helps avoid memory leaks. What should I do if I need cross-references in the classes? If you have circular references like this, one object should hold a weak_ptr to the other, not a shared

enable_shared_from_this (c++0x): what am I doing wrong?

送分小仙女□ 提交于 2019-11-27 06:46:33
问题 I'm just toying around with the smart pointers in the upcoming new c++ standard. However I fail to grasp the usage of the shared_from_this function. Here is what I have: #include <iostream> #include <memory> class CVerboseBornAndDie2 : public std::enable_shared_from_this<CVerboseBornAndDie2> { public: std::string m_Name; CVerboseBornAndDie2(std::string name) : m_Name(name) { std::cout << m_Name << " (" << this << ") is born!" << std::endl; } virtual ~CVerboseBornAndDie2() { std::cout << m

Is auto_ptr deprecated?

空扰寡人 提交于 2019-11-27 06:43:05
Will auto_ptr be deprecated in incoming C++ standard? Should unique_ptr be used for ownership transfer instead of shared_ptr? If unique_ptr is not in the standard, then do I need to use shared_ptr instead? David Rodríguez - dribeas UPDATE: This answer was written in 2010 and as anticipated std::auto_ptr has been deprecated. The advice is entirely valid. In C++0x std::auto_ptr will be deprecated in favor of std::unique_ptr . The choice of smart pointer will depend on your use case and your requirements, with std::unique_ptr with move semantics for single ownership that can be used inside

Why is auto_ptr being deprecated?

一笑奈何 提交于 2019-11-27 06:13:35
I heard auto_ptr is being deprecated in C++11. What is the reason for this? Also I would like to know the difference between auto_ptr and shared_ptr . The direct replacement for auto_ptr (or the closest thing to one anyway) is unique_ptr . As far as the "problem" goes, it's pretty simple: auto_ptr transfers ownership when it's assigned. unique_ptr also transfers ownership, but thanks to codification of move semantics and the magic of rvalue references, it can do so considerably more naturally. It also "fits" with the rest of the standard library considerably better (though, in fairness, some

Passing shared_ptr<Derived> as shared_ptr<Base>

怎甘沉沦 提交于 2019-11-27 06:11:09
What is the best method to go about passing a shared_ptr of a derived type to a function that takes a shared_ptr of a base type? I generally pass shared_ptr s by reference to avoid a needless copy: int foo(const shared_ptr<bar>& ptr); but this doesn't work if I try to do something like int foo(const shared_ptr<Base>& ptr); ... shared_ptr<Derived> bar = make_shared<Derived>(); foo(bar); I could use foo(dynamic_pointer_cast<Base, Derived>(bar)); but this seems sub-optimal for two reasons: A dynamic_cast seems a bit excessive for a simple derived-to-base cast. As I understand it, dynamic_pointer

Create a boost::shared_ptr to an existing variable

南笙酒味 提交于 2019-11-27 05:32:42
问题 I have an existing variable, e.g. int a = 3; How can I now create a boost::shared_ptr to a ? For example: boost::shared_ptr< int > a_ptr = &a; // this doesn't work 回答1: although you should put the variable into a managed pointer on it's creation to do it from an existing pointer. int *a=new int; boost::shared_ptr<int> a_ptr(a); That said you most definitely do not want to be putting stack variables into shared_ptr BAD THINGS WILL HAPPEN If for some reason a function takes shared_ptr and you