Topic: templated operator =
Author: sbnaran@localhost.localdomain (Siemel Naran)
Date: 1999/04/20 Raw View
On 20 Apr 99 14:19:10 GMT, Joerg Schaible <Joerg.Schaible.A@T.gft.de> wrote:
>template <class X>
>class auto_ptr
>{
>...
>auto_ptr& operator =(auto_ptr&) throw();
>template <class Y> auto_ptr& operator =(auto_ptr<Y>&) throw()
>1) Why is it necessary to define and implement an untemplated version of the
>assignment operator? It seems necessary, a coded exsample without the
>untemplated operator using MSVC6 shows, that the assignment p2=p1 is done by
>byte copy, not by calling the templated version of the operator! Why can't
>the compiler deduce the correct assignment operator? Or is it just MSVC6
>specific?
MSVC is right according to the standard. If you don't supply a copy
constructor or operator= for class Class, the compiler generates one for
you. These functions for class Class<X> are either
Class<X>::Class(const Class<X>&);
Class<X>& Class<X>::operator=(const Class<X>&);
Class<X>::Class(Class<X>&);
Class<X>& Class<X>::operator=(Class<X>&);
For some reason, a templated copy constructor or templated operator= does
not qualify as an ordinary copy constructor or ordinary operator=.
>2) The MSVC6 complains in the above exsample the sequence of the
>declarations. The templated assignment operator has to be defined before the
>untemplated, since it treats this version as template specialization.
>Looking at the code, the compiler logic seems to be right or is there any
>other comment in the standard ?
Don't know.
--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Joerg Schaible" <Joerg.Schaible.A@T.gft.de>
Date: 1999/04/20 Raw View
Hi folks
Looking at the ANSI documents 20.4.5 defining the auto_ptr interface I can
see following definitions for the operator =
template <class X>
class auto_ptr
{
...
auto_ptr& operator =(auto_ptr&) throw();
template <class Y> auto_ptr& operator =(auto_ptr<Y>&) throw()
...
};
So coding something like
...
auto_ptr<int> p1( new int(1) );
auto_ptr<int> p2;
p2=p1;
...
arises several questions:
1) Why is it necessary to define and implement an untemplated version of the
assignment operator? It seems necessary, a coded exsample without the
untemplated operator using MSVC6 shows, that the assignment p2=p1 is done by
byte copy, not by calling the templated version of the operator! Why can't
the compiler deduce the correct assignment operator? Or is it just MSVC6
specific?
2) The MSVC6 complains in the above exsample the sequence of the
declarations. The templated assignment operator has to be defined before the
untemplated, since it treats this version as template specialization.
Looking at the code, the compiler logic seems to be right or is there any
other comment in the standard ?
Found this problems using the smart pointers of
www.boost.org/libs/smart_ptr/index.htm together with MSVC6.
Greetings, J rg
--
BTW: It is normally better to answer to the group! For direct mail reply
exchange the ".A@T." by "@"
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]