So from the top-voted answer to this question, it appears that using raw pointers for storage isn't frowned upon so much. However, then what is the point of a std::weak_ptr? I thought that storage was the exact purpose of that class. If so, is there any circumstance in which a raw pointer is considered good, valid code? If there isn't any, what is the point of having them in the language? Is it simply historical artifact?
Usage of raw pointers
c++;pointers
weak_ptr is solely usable in conjunction with shared_ptr. It also has a little memory overhead while some weak pointers remain (a struct on the heap holding the strong and weak refcounts).It is fine to use raw pointers if you can assure yourself of the relative lifetimes of the pointer vs. what it points to and prevent dereferencing the pointer while it dangles.The smart pointers are tools to help with that (under the hood they use raw pointers). For example unique_ptr sets the lifetime of the pointed to object the same as the pointer with extra functionality for explicit reset or release. The memory footprint for a unique_ptr is the same as a raw pointer unless you use a stateful deleter.