On Apr 28, 9:10 pm, Noah Roberts <n...@nowhere.com> wrote:
Noah Roberts wrote:
#include <iostream>
#include <memory>
struct T {};
std::auto_ptr<T> f() { return new T; }
That doesn't compile in g++. So I changed the source
according to what g++ expects and it works without craishing
in both compilers. Dunno why MSVC lets me return new T, but
it does and then can't handle it.
Looking at the expanded code (your program compiled using -E),
it looks like the VC++ library is missing an explicit on the
constructor of auto_ptr_ref, compared at least to g++. It's
actually hard to say; the standard doesn't say much of anything
about what auto_ptr_ref should look like. The class definition
show it as empty, which IIUC means that it cannot contain any
public members (and the g++ implementation is non-conforming as
well). But without the explicit, you code compiles, since the
pointer returned by new converts implicitly to an auto_ptr_ref,
and auto_ptr has a constructor which takes an auto_ptr_ref. At
any rate, the VC++ auto_ptr_ref is designed to contain a pointer
to the pointer in the auto_ptr which creates it, so it's not
surprising that funny things happen when it is initialized with
something else. (The g++ implementation works differently...
and will leak memory if the auto_ptr_ref isn't used to
initialize an auto_ptr. I don't know what the correct solution
is.)
that created it.
But that's probably too obvious. :-)