It would work, but it's not exception-safe. If any of these allocations fail, you leak all others.
It fails to compile on the MinGW port of g++ 3.4.5, the Comeau online compiler and MSVC8. The reason is that auto_ptr's constructor that takes a T* is explicit. A solution would be:
Code:
std::auto_ptr<int> pi[10] = {std::auto_ptr<int>(new int),
std::auto_ptr<int>(new int),
std::auto_ptr<int>(new int),
std::auto_ptr<int>(new int),
std::auto_ptr<int>(new int),
std::auto_ptr<int>(new int),
std::auto_ptr<int>(new int),
std::auto_ptr<int>(new int),
std::auto_ptr<int>(new int),
std::auto_ptr<int>(new int)};
This modified code should be exception safe, methinks. Nonetheless, I do not see what is the point of an array of auto_ptrs.
Incidentally, this is cross posted in Code Guru: auto_ptr array .