TAGS :Viewed: 10 - Published at: a few seconds ago

[ C++ template to reference count native types ]

I need to reference count an int (file descriptor) in C++. I was looking through new C++11 and C++14 docs to see if there was a template for reference counting. Does it really not exist as part of the new things added to C++ over the last few years, or did I miss it in the docs?

Note I'm not looking for people to provide their own solutions as part of this StackOverflow question! I only want to know if there is an "official" solution I should be using.

Answer 1

If using shared_ptr is consistent with your intent (destroy the file when reference count is 0) then that would be the way to go. You can create the original shared pointer with a custom destructor, which can be a file deleter in your use case. It also has a use_count member function in the event you want to access that.