Hi all,
I recently went to
isocpp.org and went across his discussion :
After reading most of the posts, especially the one who said that
Even in C++11, raw pointers are still perfectly valid as non-owning references to objects.
something
becomes clear in my mind. Men, if using raw pointer is still the
recommended things to do in that case, isn't it the sign that something
is missing in the standard?
And the fact is that i'm quite sure that
yes, something is missing (especially because it's not the first time I
read people posting about the use of unique/shared/raw pointers).
Also, I've tried to adress this with a new class called std::ref_ptr.
The
idea is to have a pointer wich will not take ownership, but wich can be
used for referencing object wich are owned but other pointer (both
unique and shared pointers should be supported).
This led me to propose that interface:
template<typename T>
class ref_ptr
{
ref_ptr(const unique_ptr<T>&);
ref_ptr(const shared_ptr<T>&); //can be construct from any ownership-enabled pointer. I don't know if those ctors should be made explicit though.
ref_ptr(const ref_ptr<T>&); //copy constructible
T& operator *() const;
T* operator ->() const;
explicit operator bool() const;
ref_ptr& operator =(const ref_ptr<T>&);
T* get() const;
//you can add here any method wich can prove to be usefull
};
Advantage over raw pointers:
- can't be used to create new unique/shared_ptr (at least not directly), avoiding potential programming errors.
-
need an existing unique/shared_ptr, but can't be construct from raw
pointers. This guarantees that the referenced pointer is to owned by
someone and won't be leaked.
Advantage over weak pointers:
- can be used with unique_ptr.
-
no need to cast the pointer and construct a new one from it, resulting
in better performance. This class will have only one field wich is the
pointer itself. This will enable her to perform the same as raw pointers
does.
Note :
- Since this class does not deal with
ownership, referenced object must outlive the pointer itself. Otherwise,
the ref_ptr will become invalid and derefencing it will lead to
undefined behaviour.
- I did not add move constructors because I don't think it will any functionnality/benefit over copy constructor.
Uses cases :
- You want to reference an existing object wich you know will still be valid once you will have done with it : use ref_ptr
- You want to reference an existing object, but you don't know if he will still be valid when you'll need it. 2 possibility :
--> you want your referenced object to still be valid : use
shared_ptr. (having this still valid means that you wanna deal with
ownership afterall).
--> your object can become invalid, you can deal with it (typically, cache systems) : use weak_ptr.
What do you think of this?
PS:
What about std::unique_ptr&?
Another
solution for this could be to use reference to std::unique_ptr (or
eventually shared one). While I didn't test it, I do believe that a
reference to an existing pointer could become invalid at any time when
the pointer is moved for example. Can someone confirm me that?