dragoncoder wrote:
Quote:
>
I am really surprised how pi2 = source ( 5 ) working with the help of
auto_ptr_ref ( or for that matter auto_ptr <intpi1 ( source ( 5 ) )
). Can someone please explain what is happening behind the scene ?
>
1) pi2 = source ( 5 );
The code compiles because of operator=(auto_ptr_ref<T>) that was added
to allow usage like the one that you mentioned.
This assignment operator was a late addition for auto_ptr that has been
accepted to TC1:
http://www.open-std.org/jtc1/sc22/wg...fects.html#127
2) const auto_ptr<int>& r = source(5)
The current standard requires a viable copy constructor in order to
bind a class rvalue (e.g. return value from function) to a const
reference since additional temporary might be involved.
The future standard will probably relax this (frustrating) requirement
to only allow direct binding, see:
http://www.open-std.org/jtc1/sc22/wg...fects.html#391
My suggestion (DR #462) for fixing auto_ptr actually seems to allow
this case with many highly compliant compliers.
Notice that the copy semantics in C++ can be extremely subtle and there
are several simple examples for which even highly compliant compliers
completely disagree (sometimes even inconsistent them self).
Personally, I prefer regulate the code with facilities that have "less"
subtleties, are more syntactically/semantically robust and easier to
understand.
Rani