```C++
class Foo {};
void deleter1(Foo* const foo) {
std::cout << "Deleter1\n";
delete foo;
}
int main() {
std::vector> vec;
std::shared_ptr ptr1(new Foo(), deleter1);
vec.push_back(std::move(ptr1));
auto deleter2 = [](Foo* const foo) {
std::cout << "Deleter2\n";
delete foo;
};
vec.emplace_back(new Foo(), deleter2);
}
```
```Bash
Deleter1
Deleter2
```
___
### Problem with shared_ptr
Let's look at a short story:
* Programmer1 : Why do you pass shared_ptr by copy? Passing by copy increment counter (slower then unique or raw ptr)
* Programmer2: Ok, so I will pass it by const reference instead! And avoid unnecessary incrementation of the control block.
* Programmer1: So if you don't need to copy it (don't need to have 2 owners) why don't use unique_ptr?
*
Reassume: shared_ptr should be use
only when given resource need to have few owners (very rare situation). In other case use always unique_ptr!
___
### `std::shared_ptr<>` cyclic dependencies
* What happens here?
```cpp
#include
struct Node {
std::shared_ptr child;
std::shared_ptr parent;
};
int main () {
auto root = std::shared_ptr(new Node);
auto child = std::shared_ptr(new Node);
root->child = child;
child->parent = root;
}
```
Memory leak!