public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Help: auto_ptr conversion confusion
@ 2002-10-11  9:22 Kristian Spangsege
  2002-10-11  9:50 ` Joshua Nye
  2002-10-11 13:02 ` John Love-Jensen
  0 siblings, 2 replies; 3+ messages in thread
From: Kristian Spangsege @ 2002-10-11  9:22 UTC (permalink / raw)
  To: gcc-help, gcc-help

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: auto_ptr conversion confusion
  2002-10-11  9:22 Help: auto_ptr conversion confusion Kristian Spangsege
@ 2002-10-11  9:50 ` Joshua Nye
  2002-10-11 13:02 ` John Love-Jensen
  1 sibling, 0 replies; 3+ messages in thread
From: Joshua Nye @ 2002-10-11  9:50 UTC (permalink / raw)
  To: Kristian Spangsege, gcc-help

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
>
>


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: auto_ptr conversion confusion
  2002-10-11  9:22 Help: auto_ptr conversion confusion Kristian Spangsege
  2002-10-11  9:50 ` Joshua Nye
@ 2002-10-11 13:02 ` John Love-Jensen
  1 sibling, 0 replies; 3+ messages in thread
From: John Love-Jensen @ 2002-10-11 13:02 UTC (permalink / raw)
  To: Kristian Spangsege, gcc-help

Hi Kristian,

Even though a "B" is an "A".  (Structs inherit their superclasses as public
by default.)  This "auto_ptr<B>" is not one an "auto_ptr<A>".

Fix your code like this:

 1 #include <memory>
 2 using namespace std;
 3
 4 struct A {};
 5 struct B: A {};
 6
 7 auto_ptr<A> f()
 8 {
 9   auto_ptr<A> b(new B);
10   return b;
11 }
12
13 int main()
14 {
15   auto_ptr<A> a = f();
16   return 0;
17 }

And it should work, at least with GCC 3.2.

--Eljay


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2002-10-11 20:02 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-10-11  9:22 Help: auto_ptr conversion confusion Kristian Spangsege
2002-10-11  9:50 ` Joshua Nye
2002-10-11 13:02 ` John Love-Jensen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).