Back on my crazy AutoArray thingy... (quoting important bits from there:
class AutoArray
{
void * buffer;
public:
//Creates a new empty AutoArray
AutoArray();
//std::auto_ptr copy semantics
AutoArray(AutoArray&); //Note it can't be const because the "other" reference
//is null'd on copy...
AutoArray& operator=(AutoArray);
~AutoArray();
//Nothrow swap
// Note: At the moment this method is not thread safe.
void Swap(AutoArray&);
};
)
Anyway, trying to implement the copy constructor. There's a piece of client code (not yet committed into bitbucket because it won't build) that looks like this:
AutoArray NtQuerySystemInformation(...) { ... };
AutoArray systemInfoBuffer = NtQuerySystemInformation(...);
This fails because the copy constructor takes a non-const
reference as an argument .... but I don't see how you could modify the copy constructor to take a const
reference, given that the source AutoArray
used in the assignment is modified (and therefore wouldn't be const
). You can't modify things to use pass by value of course, because it's the copy constructor and that'd be an infinite loop!
If I was using auto_ptr
, this would be valid:
std::auto_ptr NtQuerySystemInformation(...) { ... };
std::auto_ptr systemInfoBuffer = NtQuerySystemInformation(...);
How then, can a class with auto_ptr
's copy semantics be possible?
auto_ptr
's copy ctor works by taking ownership away from the passed-in object. This is also a big part of the reason whyauto_ptr
can't be used in avector
or other STL collections.This is not how most copy constructors work. Usually your copy constructor will just clone the passed-in object, so you can pass a const reference to it. But
auto_ptr
doesnt do this. It actually modified the original object. In this sense, its only a copy constructor by name, not by semantics.EDIT2:
I'm trying to boil this down a bit. So effectively you're trying to do something with your class that will allow syntax similar to this:
...and you're wondering how to get the
s = gimme()
part to work. Right?The secret here is in a proxy class,
auto_ptr_ref
described by the Standard in 20.4.5 whose purpose is "to allow auto_ptr objects to be passed to and returned from functions.":auto_ptr
uses a dirty trick.I'll use a dumbed-down class named
auto_int
to demonstrate just the copy construction functionality without bringing in any complexities introduced by templates or inheritance. I think the code is mostly correct, but it's untested. Our basicauto_int
look something like this:With this basic
auto_int
, we can't copy a temporary object. Our goal is to be able to write something like:What we can do is use a helper class. For
auto_ptr
, this is calledauto_ptr_ref
. We'll call oursauto_int_ref
:Basically, an instance of this class just stores a pointer to an
auto_int
and allows us to use it as a "reference" to anauto_int
.Then in our
auto_int
class we need two additional functions. We need another constructor that takes anauto_int_ref
and we need a conversion operator that allows anauto_int
to be implicitly converted to anauto_int_ref
:This will allow us to "copy" a temporary while still having the copy constructor take a non-const reference. If we look again at our sample code:
What happens is we construct a new temporary
auto_int
and passnew int()
to the constructor that takes anint*
. This temporary is then converted to anauto_int_ref
that points to it, using theoperator auto_int_ref()
, and theauto_int
constructor that takes anauto_int_ref
is used to initializep
.There is no implicit conversion from
T*
tostd::auto_ptr<T>
. I'm guessing you do have an implicit conversion constructor fromNTSTATUS
handle toAutoArray
. But if that conversion creates a temporary, that temporary can't be copied.If you use direct initialization rather than copy initialization, your "problem" may go away.