Notes

Thoughts, write-ups, and things worth remembering.

unique_ptr

#cpp#memory Updated Sep 16, 2026

Exactly one owner, always. Copy constructor and copy assignment are deleted — only move works.

Create it

auto node = std::make_unique<TranslateNode>(1.0f, 0.0f);

Prefer make_unique over raw new. One call, no chance of leaking if something throws mid-expression.

Passing it to a function: by value, not by reference

Node& addChild(unique_ptr<Node> child);   // correct
signatureaccepts rvalue?can move-from inside?
unique_ptr<T>&no-
const unique_ptr<T>&yesno
unique_ptr<T>yesyes

Only pass-by-value satisfies both “accepts a temporary directly” and “can actually take ownership out of it.”

std::move

unique_ptr<Node> temp = std::make_unique<TranslateNode>(1, 0);
root->addChild(std::move(temp));   // temp is null from here on

Needed only for a named variable — a temporary is already an rvalue and skips this. After the move, temp is null; its destructor still runs at scope end, but deletes nothing.

Gotcha: get() before move()

Node* raw = child.get();
children.push_back(std::move(child));   // child is null now
return *raw;                             // still valid - raw pointed at
                                          // the object, not the wrapper

Grab the raw pointer first. Once moved, the unique_ptr variable is empty — calling .get() on it afterward just hands you nullptr.

Smart pointers
shared_ptr
Const keyword