smart pointers

RAII wrappers around raw pointers that automatically manage memory. unique_ptr for exclusive ownership, shared_ptr for shared.

Syntax

cpp
auto p = std::make_unique<Type>(args);
auto s = std::make_shared<Type>(args);

Example

cpp
#include <memory>

auto up = make_unique<int>(42);
cout << *up;  // 42, freed automatically

auto sp1 = make_shared<string>("hello");
auto sp2 = sp1;  // shared ownership
cout << sp1.use_count(); // 2