Title: Theory / Unimportant / CS / Programming / C++ / Effective modern / 04 Smart pointer
Tags: automatedNewLineAfterCommaSpace

Question: What are the issues with pointers
Answer: no way to know if it point to a single object or an array
Page: 117
Chapter: 4
Answer2: No way to know whether it's dangling
Answer3: No way to know whether there are other owners of it

Question: What are the issues with auto_ptr
Answer: its copy operators actually move. Hence destroy the copied value
Page: 118
Chapter: 4
Answer2: it has no move operator, so can't be used in containers

Instruction: std::unique_ptr<Foo,
  Deleter_type> (
  value,
  destructor
)
Page: 120,
  988
Language: C++
Chapter: 4,
  34
Section: 18,
  3.1
Returns: A unique_pointer with content value. Its value get destroyed with destructor

Question: When to use unique_ptr<T[]>
Answer: If you use a C-like API requiring arrays. Otherwise use a vector
Page: 124
Chapter: 4
Section: 18

Typ: std::unique_ptr<T>
Function: move
Page: 124
Function2: dereference, * and ->
Chapter: 4
Section: 18
Function3: delete (assign to nullptr)
Function4: copy into shared_ptr
Language: C++

Question: Main advantage of shared_ptr over garbage collection
Answer: deletion is deterministic
Page: 124
Chapter: 4
Section: 19

Question: What is the size of unique_ptr<T>
Answer: same size as a pointer
Page: 120
Chapter: 4
Section: 18

Question: What is the size of unique_ptr<T, destructor>
Answer: sum of pointer size and destructor size. So 2 pointer if it's the destructor is a pointer, otherwise contains the capture of the destructor
Page: 120
Chapter: 4
Section: 18

Question: What is the size of shared_ptr<T>
Answer: Size of two pointers, T and control block, by shared_ptr instance
Page: 126
Chapter: 4
Section: 19
Answer2: size of two counters, custom deleter, allocator, etc... once for all elements

Question: Why is shared_ptr costly
Answer: incrementing and decrementig the counter requires atomicity of operation, which is costly
Page: 126
Chapter: 4
Section: 19

Question: Good practice with shared_ptr construction
Answer: The constructor argument should be result of new, returned function, but not variable. Otherwise, there is a risk to create to shared_ptr to the same pointer
Page: 130
Chapter: 4
Section: 19

Name: The Curiously Recurring Template Pattern
Definition: A templated base class whose template is instantiated with the derived class
Page: 131
Chapter: 4
Section: 19
Example: class Widget: public std::enable_shared_from_this<Widget>
Abbreviation: CRTP

Typ: std::enable_shared_from_this<T>
Meaning: A class Foo inheriting it, instantiated with Foo, contains a shared_ptr to itself
Page: 131
Language: C++
Chapter: 4
Section: 19

Instruction: shared_from_this()
Page: 131f
Language: C++
Chapter: 4
Section: 19
Returns: a shared_ptr to this
Variables: std::enable_shared_from_this<T>

Question: Best practice with std::enable_shared_from_this<T>
Answer: Make constructor private and use factor that returns shared_ptr
Page: 132
Chapter: 4
Section: 19

Question: What are the uses of weak_ptr
Answer: In an observer pattern, keep a pointer to the listener, that automatically allow to know when listener disappear
Page: 138
Chapter: 4
Section: 20
Answer2: In a tree, keep a smart pointer to the parent that do not require ownership
Answer3: Create a cache from a database/network. AS long as an object exists, ensure to always use the same instance in memory each time it is requested, and still free it when it becomes useless

Instruction: std::weak_ptr(shared_pointer)
Page: 135
Language: C++
Chapter: 4
Section: 20
Returns: A weak ointer pointing to the same element as shared_pointer

Instruction: weak_pointer.expired()
Page: 135
Language: C++
Chapter: 4
Section: 20
Returns: Whether weak_pointer doesn't point to an object

Instruction: weak_pointer.lock()
Page: 135
Language: C++
Chapter: 4
Section: 20
Returns: A shared pointer pointing to the same thing as weak_pointer

Instruction: std::make_unique<T>(args)
Page: 135
Language: c++
Chapter: 4
Section: 21
Returns: A unique_ptr to `new T(args)`
Assuming: c++ at least 14

Question: Why to prefer make_unique/shared over creating a smart pointer with new
Answer: std::unique_ptr<Widget> upw2(new Widget); requires code duplication of type
Page: 140
Chapter: 4
Section: 21
Context: C++
Answer2: If there is a function call inside another function call (as would be the case with a new inside a function call), the order of execution is non deterministic. In case of exception, the new object will not be in a shared pointer, so won't be destroyed
Answer3: make_shared<T> requires a single memory allocation for both the T object and the pointer meta data. Making it more efficient
Answer4: make_shared/unique ensure that the new value is not assigned to a variable, and so there is no risk that the variable has a dangling pointer or delete the value by accident

Question: Why to prefer std::unique/shared_pointer<T>(new(...)) over  make_unique/shared over creating a smart pointer with new
Answer: make_unique/shared do not allow for a custom deleter
Page: 140
Chapter: 4
Section: 21
Context: C++
Answer2: make_unique/shared only allow initializer with parenthesis. If you want to uses curly braces, you must create the std::initializer_list explicitly
Answer3: Custom allocator and deallocator do not interact nicely with stl::allocate_shared
Answer4: std::make_unique/shared create T and the control block at the same memory. Which mean that memory can not really be freed all weak_ptr are freed. If T is big, it can be costly; in this case having two separate blocks of memory is preferable

Name: Pointer to implementation
Definition: Having a declaration of a class whose only member is a pointer to a structure which contains the actual relevant member
Page: 147
Chapter: 4
Section: 22
Context: C++
Abbreviation: Pimpl
Intuition: By delegating the underlying member to an incomplete pointer, we do not have to import anything, avoiding to need recompilation when the underlying implementation change

Name: An incomplete type
Definition: A type declared but not defined
Page: 147
Chapter: 4
Section: 22
Context: C++

Question: What to do if there is pImpl with std::unique_ptr
Answer: All special member function should be defined explicitly
Page: 156
Chapter: 4
Section: 22
Context: C++