Topic: auto_ptr::operator= const auto_ptr?


Author: jpotter@falcon.lhup.edu (John Potter)
Date: 1998/08/15
Raw View
hinnant@_anti-spam_lightlink.com (Howard Hinnant) wrote:
: What should happen when you do this:
: auto_ptr<int> source();
: int main()
: {
:    auto_ptr<int> p;
:    p = source();
: }

I know that auto_ptr has gone through a lot of wars, but we now have a
standard.  I'm curious about an answer to this question.  There seems
to be a deviation from the usual situation where construction and
assignment support the same parameters (copy value excepted).  Maybe
the subject should have been auto_ptr<> = rvalue.

 auto_ptr<X> p(auto_ptr<X>());  // works
 auto_ptr<X> q = auto_ptr<X>(); // works ? It's the same here
 p = auto_ptr<X>();             // error

If Howard's reading is correct, the rational should be educational for
many of us.

John



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: hinnant@_anti-spam_lightlink.com (Howard Hinnant)
Date: 1998/08/09
Raw View
Hi,

What should happen when you do this:

auto_ptr<int> source();

int main()
{
   auto_ptr<int> p;
   p = source();
}

The way I read the current standard is a compile time error on the 2nd
line of main.  But this works:

auto_ptr<int> source();

int main()
{
   auto_ptr<int> p(source());
}

The issue is that the copy constructor and operator= take non-const
references so when a temporary is the argument it must take a trip through
auto_ptr_ref.

There is an auto_ptr constructor (non explicit) that takes an auto_ptr_ref
(which makes the latter work), but an operator= that takes an auto_ptr_ref
is missing (which would make the former work).  Is this an intentional
design?  If so, what is the reasoning?

Or am I just reading incorrectly?

Thanks,
-Howard


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]