Thread-safe reference-counted smart pointer in C++

0
Remix

Implement a reference-counted smart pointer class template in C++ from scratch, in the spirit of std::shared_ptr. Several owners can share one object; it is destroyed exactly once, when the last owner lets go.

Write the constructor, destructor, copy and move constructors and assignments, get, operator* and operator->, and use_count. Then make the count safe across threads with atomics, explain the memory ordering you need, and discuss self-assignment, exceptions and what a weak pointer would add.

NvidiaCodingLast asked Sep 2026
No answer yet. Write the first one; the best answer gets endorsed.