mirror of
https://github.com/cpp-best-practices/cppbestpractices.git
synced 2025-12-17 19:24:36 +03:00
If possible prefer unique_ptr to shared_ptr because it is more efficient
This commit is contained in:
@@ -205,6 +205,14 @@ std::shared_ptr<ModelObject_Impl>(new ModelObject_Impl());
|
|||||||
std::make_shared<ModelObject_Impl>(); // (it's also more readable and concise)
|
std::make_shared<ModelObject_Impl>(); // (it's also more readable and concise)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Prefer `unique_ptr` to `shared_ptr`
|
||||||
|
|
||||||
|
If possible use `unique_ptr` instead of `shared_ptr`. The `unique_ptr` does not need to keep track of its copies because it is not copyable. Because of this it is more efficient than the `shared_ptr`. Equivalent to `shared_ptr` and `make_shared` you should use `make_unique` to create the `unique_ptr`:
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
std::make_unique<ModelObject_Impl>();
|
||||||
|
```
|
||||||
|
|
||||||
### Get rid of std::endl
|
### Get rid of std::endl
|
||||||
|
|
||||||
`std::endl` implies a flush operation. It's equivalent to `"\n" << std::flush`.
|
`std::endl` implies a flush operation. It's equivalent to `"\n" << std::flush`.
|
||||||
|
|||||||
Reference in New Issue
Block a user