diff --git a/src/data/roadmaps/cpp/content/unique_ptr@k9c5seRkhgm_yHPpiz2X0.md b/src/data/roadmaps/cpp/content/unique_ptr@k9c5seRkhgm_yHPpiz2X0.md index e69de29bb..1cd8dfede 100644 --- a/src/data/roadmaps/cpp/content/unique_ptr@k9c5seRkhgm_yHPpiz2X0.md +++ b/src/data/roadmaps/cpp/content/unique_ptr@k9c5seRkhgm_yHPpiz2X0.md @@ -0,0 +1,22 @@ +# unique_ptr + +One of C++'s main features includes variants of the normal *raw* C pointers. One of these is the `unique_ptr`, which is a type of smart pointer that claims exclusive ownership over a value. + +These types of pointers **can be moved** (`std::move`), but not **copied** and are automatically deleted when out of scope. The recommended way to create a `unique_ptr` is using `std::make_unique`. + +```cpp +#include +#include + +int main() { + std::unique_ptr uptr = std::make_unique(10); + std::cout << *uptr << std::endl; + + std::unique_ptr uptr2 = uptr; // compile error + std::unique_ptr uptr2 = std::move(uptr); // transferring ownership +} +``` + +- [@official@std::unique_ptr - Detailed Reference](https://en.cppreference.com/w/cpp/memory/unique_ptr) +- [@article@Smart Pointers – unique_ptr](https://www.learncpp.com/cpp-tutorial/unique-ptr/) +- [@video@When should you use std::unique_ptr? - StackOverflow Discussion](https://stackoverflow.com/questions/13782051/when-should-you-use-stdunique-ptr)