Add content for unique_ptr (#8532)

pull/8607/head
Silicon27 3 days ago committed by GitHub
parent 500bd49a62
commit f2136d2207
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 22
      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 <memory>
#include <iostream>
int main() {
std::unique_ptr<int> uptr = std::make_unique<int>(10);
std::cout << *uptr << std::endl;
std::unique_ptr<int> uptr2 = uptr; // compile error
std::unique_ptr<int> 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)
Loading…
Cancel
Save