From 786222267b8046333cb1fe8b41863cb518e95891 Mon Sep 17 00:00:00 2001 From: rob100 Date: Tue, 2 Jun 2015 11:53:50 +0200 Subject: [PATCH] If possible prefer unique_ptr to shared_ptr because it is more efficient --- 07-Considering_Performance.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/07-Considering_Performance.md b/07-Considering_Performance.md index 0baaa0f..d2eb343 100644 --- a/07-Considering_Performance.md +++ b/07-Considering_Performance.md @@ -205,6 +205,14 @@ std::shared_ptr(new ModelObject_Impl()); std::make_shared(); // (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(); +``` + ### Get rid of std::endl `std::endl` implies a flush operation. It's equivalent to `"\n" << std::flush`.