Having fun with C++'s auto_ptr

Sometimes RAII is difficult.

The following code compiles without errors or warnings on Windows using Visual Studio 2005:

However, it throws an exception just at the end of execution. Hmm, strange. Compiling it using GCC fails—it complains about the lack of an applicable operator= function.

This is what the assignment should look like:

fp = std::auto_ptr<Test::Foo>(new Test::Foo);

With that change GCC accepts it, and Microsoft’s compiler generates exception-free code.

Leave a comment