Topic: once again: std::auto_ptr


Author: Olaf Krzikalla <Entwicklung@reico.de>
Date: Fri, 5 Oct 2001 17:47:32 GMT
Raw View
Hi,

Albrecht Fritzsche pointed out to me, that the following don't compile
due to an ambiguity:

struct A {};
struct B : A {};

std::auto_ptr<A> foo ()
{
  std::auto_ptr<B> b (new B);
  return b;
}


Altough I understand the technical reason, I don't think, that this
behaviour is intended.
Question: what would we loose, if we remove the conversion-ctor:

  template<class U> auto_ptr (auto_ptr<U>& src);

?

Best regards
Olaf Krzikalla

---
[ 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://www.research.att.com/~austern/csc/faq.html                ]





Author: "Roger Orr" <rogero@howzatt.demon.co.uk>
Date: Sun, 7 Oct 2001 21:39:05 GMT
Raw View
Olaf Krzikalla wrote in message <3BBD9DFD.F2D0147C@reico.de>...
>Hi,
>
>Albrecht Fritzsche pointed out to me, that the following don't compile
>due to an ambiguity:
>
>struct A {};
>struct B : A {};
>
>std::auto_ptr<A> foo ()
>{
>  std::auto_ptr<B> b (new B);
>  return b;
>}
>
>
>Altough I understand the technical reason, I don't think, that this
>behaviour is intended.


Well, I hesitate to make _any_ comment on auto_ptr conversions but here we
go :-)

As far as I can tell, the intention of the standard is that code like this
_ought_ to work.
See the long discussion in
http://www.awl.com/cseng/titles/0-201-63371-X/auto_ptr.html

In particular the final example: Copy-initialization, base-from-derived,
e.g.

struct Base {};
struct Derived : Base {};

std::auto_ptr<Derived> source();
void sink( std::auto_ptr<Base> );
int main()
{
  sink( source() );
}

This should compile according to the article and what I understand of the
purport of auto_ptr/auto_ptr_ref.
I believe it is an equivalent conversion to the one you show.

However I find by experimentation that this code compiles on, for example,
gcc 2.95.3-4 and MSVC .NET beta 2 but not on, for example, Comeau 4.2.45.1

"26624.c", line 12: error: class "std::auto_ptr<Base>" has no suitable copy
          constructor
    sink( source() );
          ^
The behaviour hinges on conversions to and from auto_ptr_ref.
Reading the 'memory' header file which came with gcc I find a comment next
to auto_ptr_ref:-
"most present-day compilers do not support the language features that these
conversions rely on".

Which compiler(s) was your ambiguity with?

Roger Orr

PS
As far as I can tell
http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/lwg-defects.html#127
is not relevant to this case.
--
MVP in C++ at www.brainbench.com



---
[ 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://www.research.att.com/~austern/csc/faq.html                ]





Author: Olaf Krzikalla <Entwicklung@reico.de>
Date: Mon, 8 Oct 2001 19:02:16 GMT
Raw View
Hi,

Roger Orr wrote:
> Well, I hesitate to make _any_ comment on auto_ptr conversions but here we
> go :-)
I can understand that :-)


> As far as I can tell, the intention of the standard is that code like this
> _ought_ to work.
> See the long discussion in
> http://www.awl.com/cseng/titles/0-201-63371-X/auto_ptr.html
I've read this before, but the original example wasn't mentioned there.


> In particular the final example: Copy-initialization, base-from-derived,
> e.g.
>
> struct Base {};
> struct Derived : Base {};
>
> std::auto_ptr<Derived> source();
> void sink( std::auto_ptr<Base> );
> int main()
> {
>   sink( source() );
> }
>
> This should compile according to the article and what I understand of the
> purport of auto_ptr/auto_ptr_ref.
> I believe it is an equivalent conversion to the one you show.
Yes, it compiles. But it isn't fully equivalent. My example tries to
convert a lvalue, while the one in the article converts a rvalue. The
problem is an ambiguity between the conversion-ctor and the
conversion-operator.

> Which compiler(s) was your ambiguity with?
I've tried it with BCB5 and I was told, that Comeau also complains (in
fact, that is correct behaviour!).

So again the questions:
Is the conversion-ctor only there due to historical reasons?
What happens, if the conversion-ctor is removed from auto_ptr?
Can anyone give an example, that would not compile without the
conversion-ctor?


Best regards
Olaf Krzikalla

---
[ 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://www.research.att.com/~austern/csc/faq.html                ]





Author: "David Abrahams" <david.abrahams@rcn.com>
Date: Mon, 8 Oct 2001 20:17:07 GMT
Raw View
"Roger Orr" <rogero@howzatt.demon.co.uk> wrote in message
news:1002413779.17473.0.nnrp-02.9e98aa01@news.demon.co.uk...
>
> As far as I can tell, the intention of the standard is that code like this
> _ought_ to work.
> See the long discussion in
> http://www.awl.com/cseng/titles/0-201-63371-X/auto_ptr.html
>
> In particular the final example: Copy-initialization, base-from-derived,
> e.g.
>
> struct Base {};
> struct Derived : Base {};
>
> std::auto_ptr<Derived> source();
> void sink( std::auto_ptr<Base> );
> int main()
> {
>   sink( source() );
> }
>
> This should compile according to the article and what I understand of the
> purport of auto_ptr/auto_ptr_ref.
> I believe it is an equivalent conversion to the one you show.
>
> However I find by experimentation that this code compiles on, for example,
> gcc 2.95.3-4 and MSVC .NET beta 2 but not on, for example, Comeau 4.2.45.1
>
> "26624.c", line 12: error: class "std::auto_ptr<Base>" has no suitable
copy
>           constructor
>     sink( source() );
>           ^
> The behaviour hinges on conversions to and from auto_ptr_ref.
> Reading the 'memory' header file which came with gcc I find a comment next
> to auto_ptr_ref:-
> "most present-day compilers do not support the language features that
these
> conversions rely on".

That's no longer true, in my experience.

> Which compiler(s) was your ambiguity with?
>
> Roger Orr
>
> PS
> As far as I can tell
> http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/lwg-defects.html#127
> is not relevant to this case.

No, however
http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/cwg_defects.html#84
is relevant, and explain's Comeau's reluctance to compile your example.

--
===================================================
  David Abrahams, C++ library designer for hire
 resume: http://users.rcn.com/abrahams/resume.html

        C++ Booster (http://www.boost.org)
          email: david.abrahams@rcn.com
===================================================



---
[ 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://www.research.att.com/~austern/csc/faq.html                ]