This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: auto_ptr error is rather confusing
- To: gcc at gcc dot gnu dot org
- Subject: Re: auto_ptr error is rather confusing
- From: Ross Smith <ross dot s at ihug dot co dot nz>
- Date: Wed, 09 Aug 2000 09:47:31 +1200
- Organization: Ihug
- References: <20000808171715.A5831@tkd-fires-01.ja.jpmorgan.com>
Anthony Heading wrote:
>
> Hi,
> The following doesn't appear to compile using either 2.95.2 or
> CodeSourcery's splendid web snapshot machine (though the
> diagnostics are very different).
>
> Yet Sun CC 5 and MSVC 6 both compile it without complaint.
>
> I'm quite prepared to believe that if I were smart enough
> to be able to make sense of the X3J16/WG21 documentation,
> then it would be clear why I shouldn't expect it to work,
> but perhaps then an improved diagnostic or a comment in
> some documentation or something would be helpful.
>
> #include <memory>
> int main()
> {
> std::auto_ptr<int> x;
> x = std::auto_ptr<int>(new int);
> return 0;
> }
Strictly speaking, GCC is right and Sun and MSVC are wrong, but I'm
prepared to be forgiving on this one since it was a very late addition
to the standard. The relevant bit is 13.3.3.1.4 para 3, which boils down
to "you can't bind a non-const reference to a temporary". auto_ptr's
assignment operator takes a non-const reference on its RHS, since it
removes ownership of the target object, so you can't put a temporary
there. The right way to do this is "x.reset(new int);".
GCC 2.95.2 gives:
> foo.cpp: In function `int main()':
> foo.cpp:5: initialization of non-const reference type `class auto_ptr<int> &'
> foo.cpp:5: from rvalue of type `auto_ptr<int>'
> /usr/lib/gcc-lib/i686-pc-cygwin/2.95.2/../../../../include/g++-3/memory:40: in passing argument 1 of `auto_ptr<int>::ope
rator =(auto_ptr<int> &)'
which seems pretty clear to me.
The Code Sourcery test box (2.96 20000722) gives:
> /tmp/@12950.1: In function `int main ()':
> /tmp/@12950.1:5: no match for `auto_ptr & = auto_ptr'
> /usr/local/include/g++-3/memory:40: candidates are: auto_ptr<_Tp>
> &auto_ptr<_Tp>::operator= (auto_ptr<_Tp> &) [with _Tp = int]
> /usr/local/include/g++-3/memory:48: auto_ptr<_Tp>
> &auto_ptr<_Tp>::operator= (auto_ptr<_Tp1> &) [with _Tp1 = int, _Tp =
> int]
which is a lot less clear. A language lawyer could figure out what the
problem was from that, but it doesn't explicitly refer to binding a
non-const reference to an rvalue so it's a lot harder to spot what's
going on. It looks as though somebody's experimenting with new error
message formats.
--
Ross Smith <ross.s@ihug.co.nz> The Internet Group, Auckland, New Zealand
========================================================================
"C++ is to programming as sex is to reproduction. Better ways might
technically exist but they're not nearly as much fun." -- Nikolai Irgens