Logo of inline category

C++ typedef pointer template

Designing a C++ API, I came across an interesting problem lately. A member function was returning a pointer a newly created object and it wasn’t apparent (without reading the documentation) if the returned pointer was still managed by the creator class or not. I figured there should be an easy way to tell the API user that a returned pointer was either safe or unsafe to delete…

undeletable_ptr

So I went consulting the internet and found this code by “Doctor Smith” (whose full name I could not find after a reasonable amount of stalking) who in turns credits the book “Unsmart Pointers, Part I: Preventing and catching errors” by Christopher Diggins. It’s a short header containing a template class  undeletable_ptr , wrapping a single pointer and causing the compiler to throw errors should you try to delete it (because it is not a pointer but a real object). It’s quite “dumb” compared to a true smart pointer, it does not cause the object pointed to to remain alive nor does it check whether it has been deleted — but it ensures at least accidental deletion of the object pointed to and it does tell the reader that the pointer is only borrowed.

After using  undeletable_ptr for a few weeks (and basically replacing all my raw pointers with it), I feel like the disadvantages of this approach have finally outweigh the advantage.
First off, even though the object has the  operator ->  properly overloaded so you can use it like a pointer, it is not a pointer and my IDE (QtCreator) refused to treat it as such. Next I had to patch the code linked above with my own (trivial) implementations of the  ==!=  and truth-operators. Besides, I have rarely seen this method in use anywhere else meaning either that this is a really smart or (more likely) a somewhat funky idea. Also the unwieldy long name is cluttering my interfaces…
Eventually I concluded that further patching was probably not worthwhile.
You can however still see my final implementation of the  undeletable_ptr class here:

the typedef pointer template

In the end, calling delete on a pointer that you are not sure about owning in the first place is like running with (acid drenched, flaming hot) scissors. And if you wanted to make the API completely fool-proof, there are still enough ways to unshell the raw pointer from an  undeletable_ptr , wreaking havoc all over the place.
All I wanted was to tell the user that the pointer returned from int* getNumberA();  was not safe to delete, while the one from int* getNumberB() comes with ownership and responsibility for removal.

My next idea was to use typdef like this:

but of course, this would get tedious if repeated for all sorts of types:  owned_ptr_double, owned_ptr_Foo, owned_ptr_Bar etc.
Then I hit upon this entry on stackoverflow. Turns out since C++11 you can now easily template pointer typdefs:

In this case  borrowed_ptr<int>  works just like a pointer, because it is a pointer, while still giving more information about its intended usage.
Just what I wanted!

And as final tip of this post: const owned_ptr<Foo> bar = new Foo(); does not mean, that bar is unable to access non-const public methods of class Foo!
What it means instead is that you cannot assign another value to it.

If you want the equivalent of const Foo* bar = new Foo(); you have to write:
owned_ptr<const Foo> bar = new Foo(); .

And that is all. Let me know if I made a mistake or there is an even better way to name your pointers.