This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: auto_ptr conversion confusion
- From: "Joshua Nye" <josh at boxcarmedia dot com>
- To: "Kristian Spangsege" <kks at framfab dot dk>, <gcc-help at gcc dot gnu dot org>
- Date: Fri, 11 Oct 2002 12:50:13 -0400
- Subject: Re: auto_ptr conversion confusion
- References: <3DA6FAD3.70904@framfab.dk>
I'm not sure about the standard but Intel's C++ compiler barfs on this too.
[josh@lars src]$ icc -o ap ap.cc
ap.cc
ap.cc(15): error: more than one user-defined conversion from
"std::auto_ptr<B>" to "std::auto_ptr<A>" applies:
function template "std::auto_ptr<_Ty>::operator
std::auto_ptr<_Other>() [with _Ty=B]"
function template
"std::auto_ptr<_Ty>::auto_ptr(std::auto_ptr<_Other> &) [with _Ty=A]"
auto_ptr<A> a = f();
^
compilation aborted for ap.cc (code 2)
--josh
----- Original Message -----
From: "Kristian Spangsege" <kks@framfab.dk>
To: <gcc-help@gcc.gnu.org>; <gcc-help@gcc.gnu.org>
Sent: Friday, October 11, 2002 12:22 PM
Subject: Help: auto_ptr conversion confusion
> Hi,
>
> There seems to be a problem with the std::auto_ptr template.
>
> I am unsure if it is an error in the implemetation of auto_ptr or if
> it is an error in the G++ frontend, or just an incorrect library
> documentation.
>
> The aparent problem is that the following selfcontained program
> does not compile with GCC3.2:
>
> 1 #include <memory>
> 2 using namespace std;
> 3
> 4 struct A {};
> 5 struct B: A {};
> 6
> 7 auto_ptr<B> f()
> 8 {
> 9 auto_ptr<B> b;
> 10 return b;
> 11 }
> 12
> 13 int main()
> 14 {
> 15 auto_ptr<A> a = f();
> 16 return 0;
> 17 }
>
> I get the following error from gcc3.2
>
> t.C: In function `int main()':
> t.C:15: no matching function for call to
> `std::auto_ptr<A>::auto_ptr(std::auto_ptr<A>)'
> /usr/include/g++-v3/bits/std_memory.h:122: candidates are:
> std::auto_ptr<_Tp>::auto_ptr(std::auto_ptr_ref<_Tp>) [with _Tp = A]
> /usr/include/g++-v3/bits/std_memory.h:76:
> std::auto_ptr<_Tp>::auto_ptr(std::auto_ptr<_Tp1>&) [with _Tp1 = A,
> _Tp = A]
> /usr/include/g++-v3/bits/std_memory.h:73:
> std::auto_ptr<_Tp>::auto_ptr(std::auto_ptr<_Tp>&) [with _Tp = A]
> t.C:15: initializing temporary from result of
> `std::auto_ptr<_Tp>::operator
> std::auto_ptr<_Tp1>() [with _Tp1 = A, _Tp = B]'
>
> Now, I know that I can fix the problem by changing line 15 to:
>
> 15 auto_ptr<A> a(f());
>
> But in your source level documentation you state that the
> alternative construct using = should work:
> http://gcc.gnu.org/onlinedocs/libstdc++/libstdc++-html-USERS-3.2/classst
> d_1_1auto__ptr.html#z15_3
>
>
> Could someone explain to me exactly what the semantical difference (if
> any) is between these two versions of line 15:
>
> 15 auto_ptr<A> a = f();
> 15 auto_ptr<A> a(f());
>
> Does gcc3.2 handle these alternative constructs the way it is intended
> ib the C++ standard?
>
> It seems to me that in the first case (=) the compiler is only willing
> to use a restricted set of constructors. In particular it wont use the
> following constructor anymore:
>
> auto_ptr (auto_ptr_ref< element_type > __ref) throw ()
>
> Is this a correct, and if so, is this intentional from gcc's point of
> view, and does it comply with the C++ standard?
>
> If this is intentional from gcc's point of view, then the problem is
> just that the source level documentation is imprecise (as I see it).
>
>
> Could someonw please clarify for me?
>
> Thanks in advance.
> Kristian Spangsege
>
>