I’ve heard it mentioned a couple of times that std::vector is not exception-safe when storing raw pointers and that one should use unique_ptr or shared_ptr instead.
My question is, why is std::vector not exception-safe and how do these classes fix that?
It’s not
std::vectorthat’s not exception safe, it’s using raw pointers for memory management:That has nothing to do with vector’s per se, it’s just that doing this is the exact same problem:
Both of these are fixed by using smart pointers:
(Or
shared_ptr, which is more expensive. You may also use pointer containers, from Boost.)