I usually don’t use smart pointers when implementing containers, as you show. Raw pointers (imho) are not things that should be avoided like the plague. Use the smart pointer if you want to ensure memory ownership. But usually in a container, the container has memory, which is indicated by pointers that make up the data structure.
If in your design AVLTreeNode uniquely owns its left and right children, and you want to express it with unique_ptr , that's fine. But if you prefer AVLTree have all AVLTreeNode s, and do it using raw pointers, this is also true (and I usually code it).
Believe me, I'm not an anti-smart pointer. I am the one who came up with unique_ptr . But unique_ptr is another tool in the toolbox. Having good smart pointers in the tool box is not a cure, and using them blindly for everything does not replace a meticulous design.
Refresh to reply to comment (comment field was too small):
I often use raw pointers (which are rarely owned). A good sample of my coding style exists in the open source libC ++ project. You can view the source in the View SVN section.
I prefer that each allocation of a resource be freed in the destructor somewhere due to security issues, even if the usual release occurs outside the destructor. When a distribution belongs to one pointer, a smart pointer is usually the most convenient tool in a tool box. When a distribution belongs to something larger than a pointer (such as a container or Employee class), raw pointers are often a convenient part of the data structure that makes up a larger object.
Most importantly, I never allocate any resource without knowing which object belongs to this resource, be it a smart pointer, a container or something else.
Howard hinnant
source share