Search code examples
c++11naming-conventionssmart-pointersunique-ptrapi-design

Naming APIs, to imply transfer of memory ownership?


I have a data structure. It provides APIs for adding or deleting items from its internal collection. Internally, the structure uses std::unique_ptr to maintain the lifespan of each item. This means that "adding" an item implicitly transfers ownership of the memory to my data structure.

I've documented this clearly in the comments for this API, but I was wondering; is there a de-facto name or verb I can attach to my API, to call attention to this important side-effect?

For instance: AddItem(Item* item) does not seem to adequately imply transfer of ownership. RegisterItem, CreateItem, TransferItem all seem to imply something different.


Solution

  • The way to communicate the transfer of ownership is to use a std::unique_ptr as the argument:

    AddItem(std::unique_ptr<Item> item)