agentleFS
13eholder13eholder/Modern-Cpp-Skills/m01-ownership/SKILL.md

m01-ownership

Skill14 starsChanged 9 months agoMITView on GitHub

Mastering C++ Ownership: Move Semantics, RAII, and Reference Safety. Triggers: std::move, use-after-free, double-free, dangling pointer, copy elision, RVO, reference qualifiers.

7 other projects use a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

4 rules. 0 are common, 2 are distinctive, and 2 are very specific to this project.

  • 0

    Common

    Most of the 7 edited versions we read keep these.

  • 2

    Distinctive

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Scope-bound?: Use Stack (RAII).

    line 13
  2. Very specific:

    View?: Use T* or T& (non-owning).

    line 16
  3. Distinctive:

    DO NOT write a destructor unless managing a non-RAII C handle.

    line 33
  4. Very specific:

    Never pass std::shared_ptr unless transferring shared ownership.

    line 41

“Common” is measured: kept by at least 6 in 10 of the 7 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

C++ Ownership (Move Semantics & RAII)

Core Question

Who owns this resource, and does it need to move?

In C++, ownership is a discipline, not just a compiler check. You must decide:

  • Scope-bound? Use Stack (RAII).
  • Exclusive? Use std::unique_ptr.
  • Shared? Use std::shared_ptr.
  • View? Use T* or T& (non-owning).

Error → Design Question

Compiler/Sanitizer ErrorDesign Question
Double FreeWho owns this? Did you copy a raw pointer?
Use After FreeDid a reference outlive its owner?
Memory LeakWhere is the destructor called? (Did you use new?)
Object slicingWhy are you passing by value instead of pointer/reference?
Moved-from usageWhy are you touching a variable after std::move?

Thinking Prompt

  1. Does it need a heap allocation?
    • No? → Stack value (Rule of Zero).
    • Yes? → std::unique_ptr (Rule of Zero).
    • DO NOT write a destructor unless managing a non-RAII C handle.
  2. Is it a Transfer or a Copy?
    • Transfer? → std::move.
    • Copy? → Copy Constructor (or clone() pattern if polymorphic).
  3. Is this a View?
    • Yes? → std::string_view, std::span, or const T&.
    • Never pass std::shared_ptr unless transferring shared ownership.

Trace Up / Down

  • Trace Up (Diagnosing):
    • Error: "Segfault / Heap Corruption"
    • Ask: Is there a raw pointer (T*) managing a resource?
    • Fix: Wrap in unique_ptr or vector.
  • Trace Down (Implementing):
    • Intent: "I want to pass this big object to a function and never see it again."
    • Code: void consume(BigThing t); called with consume(std::move(thing));

Quick Reference

PatternCostUse When
Value (Stack)ZeroDefault choice. Small/Medium objects.
unique_ptrZeroHeap needed. Exclusive ownership.
shared_ptrAtomic Inc/DecCyclic graph or true shared ownership.
T& (Ref)ZeroParameter passing (non-null).
T* (Ptr)ZeroParameter passing (nullable).
std::moveZeroTransferring ownership.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.