Andrew said:
Point taken; but this article is not directly about ownership transfer.
It is about the use of auto_ptr_ref proxy class to prevent copy
construction or copy assignment of a const auto_ptr.
I know, but it is a weird way to begin an article. You could say that
one of the difficulties in understanding the implementation of
std::auto_ptr is how it works with const instances, or something like
that.
I have provided a couple of references.
I misunderstood the statement, sorry.
I will refrain from introducing lvalue/rvalue terms in this article
because the terms themselves do not appear to be uniformly understood
by the C++ community (just search this newsgroup for instance).
Temporary objects, are, on the other hand, trivial to understand.
...but are not the same thing. The choice is yours to make: either be
correct wrt the standard, or use widespread terms. I would suggest
using the correct terms, while adding a note about the common usage (an
rvalue is commonly called a temporary...).
I do not understand your comment here. This concept is taken almost
straight out of Stroustrup's book, as cited on pg. 98: "References to
variables and referenced to constants are distinguished because the
introduction of a temporary in the case of variable is highly
error-prone;..."
This is true, but it is only an example. It may make sense for some
temporaries (such as exceptions) to be modified, so not all temporaries
may not be bound to a non-const reference.
Although it may be valid to modify some temporaries (because they are
not, for example, in a readonly area in memory), such as
void f(int &i);
int main()
{
f(int(3));
}
it is forbidden because it is usually a semantic error (if f() takes a
non-const reference, this is because it is meant to be modified and
"sent back" to the user).
If you can point out what is wrong or too restrictive with this
statement, I will fix it. As it stands, it may not be complete, but it
certainly is correct.
I should have said that it is partly wrong *because* is too
restrictive. "This is to prevent.." should at least be "This is to
prevent, for example, ...". However, temporaries created by a function
returning something else than a reference *are* rvalues, so your
example is indeed correct.
You explicitly talk about temporaries throughout the whole document,
but standard-wise, it is wrong. rvalues, not temporary objects, may not
be bound to non-const references. Some rvalues are temporaries and some
temporaries are rvalues.
IMHO, they are fine. If you can make a specific suggestion, that would
help.
auto_ptr(const auto_ptr_ref& rhs) throw() : ap(rhs.yp)
{ std::cerr << this << " auto_ptr constructed from " << &rhs
<< " : auto_ptr_ref -> auto_ptr" << std::endl; }
should be
auto_ptr(const auto_ptr_ref& rhs) throw()
: ap(rhs.yp)
{
std::cerr << this << " auto_ptr constructed from " << &rhs
<< " : auto_ptr_ref -> auto_ptr" << std::endl;
}
or something along these lines. Whitespace is important, especially in
a tutorial/article/whatever you call something meant to teach. Though
there may be differences between our coding conventions, the code
should be easy to read and, imo, it is not.
I think that std::cerr is better for this, since it isn't buffered.
By convention, std::cerr is used in case something bad happened and
must be reported. Since it is not buffered, system calls are made for
each output, making it more likely to appear but slowing the
application because system calls are usually expensives. Buffered
streams may not be flushed if the application terminates unexpectedly.
Since these log messages are not vital to the execution, they should go
to a buffered stream, such as std::cout or std::clog. Since std::cout
is usually used for "normal" output, std::clog is the way to go. It is
also easier to redirect logging and errors to different streams, such
as files or different output devices.
Note that is a convention, but, imo, conventions are better followed,
event in small programs.
I'll keep referring to the addresses using 3 letters/numbers. Prevents
speaking of address 90.
I understand that, but I don't get what the "a" is. I think "This is
returned as the temporary object with address ending in ab0" makes more
sense, but there may be something I don't understand.
That is the point of the sentence.
And what else is not part of "straight forward object-oriented C++"?
Jonathan