I am aware that you have to be careful with auto pointers (and why), especially with the STL. But I don’t see a problem with this:
std::map<T1, std::auto_ptr<T2> >;
Is this safe?
I see how it would break in an std::vector, because it has to copy its items from time to time, but is this also true for the value type of an std::map?
Edit: Apparently, regardless whether it’s safe, I cannot (technically) populate the map, but I’ll leave the question open for theoretical considerations. Otherwise, consider it closed.
It’s not safe. Technically,
std::auto_ptrdoesn’t meet the requirement of CopyConstructible or Assignable because copies made usingauto_ptr‘s copy constructor or copy assignment operator aren’t equivalent to the source of the copy after the copy operation. These requirements must be met for any type used with any standard container.You may find that you appear to get the behaviour you expect on one implementation for one particular use case but if you violate the requirements of the container you can’t expect your code to work in all situations.