Help: auto_ptr conversion confusion

Kristian Spangsege kks@framfab.dk
Fri Oct 11 09:30:00 GMT 2002


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




More information about the Gcc-bugs mailing list