Search code examples
shared-ptrc++11reference-counting

shared_ptr<> is not required to use reference count?


Do I understand the new Std right that shared_ptr is not required to use a reference count? Only that it is likely that it is implemented this way?

I could imagine an implementation that uses a hidden linked-list somehow. In N3291 "20.7.2.2.5.(8) shared_ptr observers [util.smartptr.shared.obs]" The note says

[ Note: use_count() is not necessarily efficient. — end note ]

which gave me that idea.


Solution

  • You're right, nothing in the spec requires the use of an explicit "counter", and other possibilities exist.

    For example, a linked-list implementation was suggested for the implementation of boost's shared_ptr; however, the proposal was ultimately rejected because it introduced costs in other areas (size, copy operations, and thread safety).