smart-pointers

Propagate constness to data pointed by member variables

浪子不回头ぞ 提交于 2019-11-28 21:15:48
It is often quite confusing to C++ newcomers that const member functions are allowed to call non-const methods on objects referenced by the class (either by pointer or reference). For example, the following is perfectly correct: class SomeClass { class SomeClassImpl; SomeClassImpl * impl_; // PImpl idiom public: void const_method() const; }; struct SomeClass::SomeClassImpl { void non_const_method() { /*modify data*/ } }; void SomeClass::const_method() const { impl_->non_const_method(); //ok because impl_ is const, not *impl_ }; However, it would sometimes be rather handy if the constness would

C++11 Smart Pointer Policies

老子叫甜甜 提交于 2019-11-28 21:09:09
As I understand it, in the current specification of C++11, one should use: std::unique_ptr<> for one owner (most of the time) std::shared_ptr<> only when there are multiple owners in acyclic structure std::weak_ptr<> sparingly only when there are cycles that need to be broken A raw pointer as a handle to memory (no ownership) when a reference would not suffice So my questions are: Are these policies sufficient or are there additional policies that I should be aware of? Are scoped_ptr<> and auto_ptr<> effectively obsolete? Are scoped_ptr<> and auto_ptr<> effectively obsolete? auto_ptr is

best practice when returning smart pointers

倾然丶 夕夏残阳落幕 提交于 2019-11-28 20:56:40
问题 What is the best practice when returning a smart pointer, for example a boost::shared_ptr? Should I by standard return the smart pointer, or the underlying raw pointer? I come from C# so I tend to always return smart pointers, because it feels right. Like this (skipping const-correctness for shorter code): class X { public: boost::shared_ptr<Y> getInternal() {return m_internal;} private: boost::shared_ptr<Y> m_internal; } However I've seen some experienced coders returning the raw pointer,

smart pointers + “this” considered harmful?

喜你入骨 提交于 2019-11-28 20:49:43
In a C++ project that uses smart pointers, such as boost::shared_ptr , what is a good design philosophy regarding use of " this "? Consider that: It's dangerous to store the raw pointer contained in any smart pointer for later use. You've given up control of object deletion and trust the smart pointer to do it at the right time. Non-static class members intrinsically use a this pointer. It's a raw pointer and that can't be changed. If I ever store this in another variable or pass it to another function which could potentially store it for later or bind it in a callback, I'm creating bugs that

What happens when using make_shared

不羁的心 提交于 2019-11-28 19:14:27
I'm interested if these two lines of code are the same: shared_ptr<int> sp(new int(1)); // double allocation? shared_ptr<int> sp(make_shared<int>(1)); // just one allocation? If this is true could someone please explain why is it only one allocation in the second line? The first case does not perform a double allocation, it performs two allocations, one for the managed object and one for the control block of the shared_ptr . For the second case, cppreference has a good explanation for why std::make_shared usually only performs one memory allocation it says ( emphasis mine going forward ): This

Deletion of pointer to incomplete type and smart pointers

前提是你 提交于 2019-11-28 18:48:12
When trying to use an auto_ptr with a type that was declared with forward-declaration, like this: class A; ... std::auto_ptr<A> a; the destructor of A is not called (apparently, because auto_ptr internally delete s the underlying pointer and the destructor for an incomplete type cannot be called). However, the same code works fine and the destructor is called when using std::shared_ptr instead of std::auto_ptr . How can that be explained? A shared_ptr can be declared with an incomplete type, yes. The type does not need to be complete until you initialize or reset it. When you initialize or

How does a reference-counting smart pointer's reference counting work?

不羁岁月 提交于 2019-11-28 18:47:05
问题 In other words, how does the implementation keeps track of the count? Is there a map-like object maintained which is accessible by all the shared_ptr instances whose key is the pointer's address and value is the number of references? If I've to implement a shared_ptr , this is the first idea that's coming to my mind. Is there a possibility for a memory leak in case of these reference-counting smart pointers? If so, how can I avoid them? 回答1: I've seen two different non-intrusive approaches to

Dynamic casting for unique_ptr

给你一囗甜甜゛ 提交于 2019-11-28 18:32:58
As it was the case in Boost, C++11 provides some functions for casting shared_ptr : std::static_pointer_cast std::dynamic_pointer_cast std::const_pointer_cast I am wondering, however, why there are no equivalents functions for unique_ptr . Consider the following simple example: class A { virtual ~A(); ... } class B : public A { ... } unique_ptr<A> pA(new B(...)); unique_ptr<A> qA = std::move(pA); // This is legal since there is no casting unique_ptr<B> pB = std::move(pA); // This is not legal // I would like to do something like: // (Of course, it is not valid, but that would be the idea)

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

落花浮王杯 提交于 2019-11-28 17:07:57
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; 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 class template. Look at their declarations: template<class T,class Deleter = std::default_delete<T> > class

Passing unique_ptr to functions

心已入冬 提交于 2019-11-28 16:58:28
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, which makes the code safer and generally better. My question is this - How should I then pass the device