Topic: template constructors
Author: "Ken Bloom" <ken195@bigfoot.com>
Date: 2000/07/26 Raw View
<assert@my-deja.com> wrote in message news:8liojh$jt9$1@nnrp1.deja.com...
> A friend and I have a debate about template copy constructors.
>
> I had thought that according to the standard a
> template copy constructor is never considered for
> copying of class objects so in effect they are useless.
>
> The code follows:
>
> template <class T>
> class C
> {
> public:
> template <class U> C(const C<U> &c) {}
> };
>
> int main()
> {
> C<int> ci;
> C<float> cf(ci);
> }
>
> Is this ill-formed, or well formed ?
This is well formed. Template copy constructors are not used for copying a
C<A> to another C<A>. (Except that this does work in MSVC++ for some
reason.)
---
[ 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: Anthony Williams <anthony_w@my-deja.com>
Date: 2000/07/27 Raw View
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
In article <8lkeas$nba$1@slb0.atl.mindspring.net>,
"Ken Bloom" <ken195@bigfoot.com> wrote:
> <assert@my-deja.com> wrote in message news:8liojh$jt9
$1@nnrp1.deja.com...
> > template <class T>
> > class C
> > {
> > public:
> > template <class U> C(const C<U> &c) {}
> > };
> >
> > int main()
> > {
> > C<int> ci;
> > C<float> cf(ci);
> > }
> >
> > Is this ill-formed, or well formed ?
>
> This is well formed. Template copy constructors are not used for
copying a
> C<A> to another C<A>. (Except that this does work in MSVC++ for some
> reason.)
This example blurs the point. The following uses the declared copy
constructor:
template<typename T>
class D
{
public:
D(const D&d){} // proper copy constructor for type D<T>
};
void func()
{
D<int> d1;
D<int> d2(d1);
}
However, the following uses the compiler-generated copy constructor:
class E
{
public:
template<typename T>(const T&e){} // template conversion constructor
};
void g()
{
E e1;
E e2(e1);
}
- --
alink@anthonyw.cjb.net -- Questions relating to ALINK
anthony@anthonyw.cjb.net -- Non-ALINK questions
anthonyw.cjb.net -- ALINK home page
PGP Key at: i3.yimg.com/3/c7e5ee24/g/68fc2307.asc
-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 6.5.1 for non-commercial use <http://www.pgp.com>
Comment: PGP Key at: http://i3.yimg.com/3/c7e5ee24/g/68fc2307.asc
iQA/AwUBOX7mEZvw+P4cG5rVEQIEoACgiKrqfMMx93wh1opv6mgxR8Xw8XUAn1+r
Xrqw1hjqTAnwQn5Cyzu9YHsS
=d3Rz
-----END PGP SIGNATURE-----
Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ 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: assert@my-deja.com
Date: 2000/07/26 Raw View
A friend and I have a debate about template copy constructors.
I had thought that according to the standard a
template copy constructor is never considered for
copying of class objects so in effect they are useless.
I had formed this opinion from reading a few
older posts in this news group and then examining
12.8 /1 and 12.8/2 of the standard where it says:
[class.copy] 12.8 Copying class objects
1 A class object can be copied in two ways, by initialization (12.1,
8.5), including for function argument passing (5.2.2) and for function
value return (6.6.3), and by assignment (5.17). Conceptually, these two
operations are implemented by a copy constructor (12.1) and copy
assignment operator (13.5.3).
2 A non template constructor for class X is a copy constructor if its
first parameter is of type X&, const X&, volatile X& or const volatile
X&, and either there are no other parameters or else all other
parameters have default arguments (8.3.6) ...
My friend has shown me code that has a template copy constructor
and clearly gets considered. This code will compile on Borland C++
Builder 4, g++ 2.95.2 and the Comeau web interface for compiler 4.2.43.
The code follows:
template <class T>
class C
{
public:
C() {}
template <class U>
C(const C<U> &c) {}
};
int main()
{
C<int> ci;
C<float> cf(ci);
}
Is this ill-formed, or well formed ?
Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ 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: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 2000/07/26 Raw View
assert@my-deja.com wrote:
>=20
> A friend and I have a debate about template copy constructors.
>=20
> I had thought that according to the standard a
> template copy constructor is never considered for
> copying of class objects so in effect they are useless.
>=20
> I had formed this opinion from reading a few
> older posts in this news group and then examining
> 12.8 /1 and 12.8/2 of the standard where it says:
>=20
> [class.copy] 12.8 Copying class objects
>=20
> 1 A class object can be copied in two ways, by initialization (12.1,
> 8.5), including for function argument passing (5.2.2) and for function
> value return (6.6.3), and by assignment (5.17). Conceptually, these two
> operations are implemented by a copy constructor (12.1) and copy
> assignment operator (13.5.3).
>=20
> 2 A non=ADtemplate constructor for class X is a copy constructor if its
> first parameter is of type X&, const X&, volatile X& or const volatile
> X&, and either there are no other parameters or else all other
> parameters have default arguments (8.3.6) ...
>=20
> My friend has shown me code that has a template copy constructor
> and clearly gets considered. This code will compile on Borland C++
> Builder 4, g++ 2.95.2 and the Comeau web interface for compiler 4.2.43.
>=20
> The code follows:
>=20
> template <class T>
> class C
> {
> public:
> C() {}
> template <class U>
> C(const C<U> &c) {}
> };
>=20
> int main()
> {
> C<int> ci;
> C<float> cf(ci);
> }
>=20
> Is this ill-formed, or well formed ?
It is well, formed, but it's no copy constructor, but a
conversion constructor. It converts C<int> to C<float>.
Note that your constructor will not be considered in the
following:
void foo()
{
C<int> ci;
C<int> Cj(ci); // uses compiler generated copy constructor,
} // not template instantiation
---
[ 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: assert@my-deja.com
Date: 2000/07/26 Raw View
Yup.. this falls under the catagory of
"posted too soon", after reflecting
on it an hour or so after the post
it became clear to me.
I was lumping all template ctors with arguments of C<T>&
in as copy ctors, but now I see that in the case
that the template parameter of the argument matches
that of the class, the template version is supressed.
Thank you for responding.
In article <397DB936.642D4D0E@physik.tu-muenchen.de>,
Christopher Eltschka <celtschk@physik.tu-muenchen.de> wrote:
> assert@my-deja.com wrote:
> >=20
> > A friend and I have a debate about template copy constructors.
> >=20
> > I had thought that according to the standard a
> > template copy constructor is never considered for
> > copying of class objects so in effect they are useless.
> >=20
> > I had formed this opinion from reading a few
> > older posts in this news group and then examining
> > 12.8 /1 and 12.8/2 of the standard where it says:
> >=20
> > [class.copy] 12.8 Copying class objects
> >=20
> > 1 A class object can be copied in two ways, by initialization (12.1,
> > 8.5), including for function argument passing (5.2.2) and for
function
> > value return (6.6.3), and by assignment (5.17). Conceptually, these
two
> > operations are implemented by a copy constructor (12.1) and copy
> > assignment operator (13.5.3).
> >=20
> > 2 A non=ADtemplate constructor for class X is a copy constructor if
its
> > first parameter is of type X&, const X&, volatile X& or const
volatile
> > X&, and either there are no other parameters or else all other
> > parameters have default arguments (8.3.6) ...
> >=20
> > My friend has shown me code that has a template copy constructor
> > and clearly gets considered. This code will compile on Borland C++
> > Builder 4, g++ 2.95.2 and the Comeau web interface for compiler
4.2.43.
> >=20
> > The code follows:
> >=20
> > template <class T>
> > class C
> > {
> > public:
> > C() {}
> > template <class U>
> > C(const C<U> &c) {}
> > };
> >=20
> > int main()
> > {
> > C<int> ci;
> > C<float> cf(ci);
> > }
> >=20
> > Is this ill-formed, or well formed ?
>
> It is well, formed, but it's no copy constructor, but a
> conversion constructor. It converts C<int> to C<float>.
> Note that your constructor will not be considered in the
> following:
>
> void foo()
> {
> C<int> ci;
> C<int> Cj(ci); // uses compiler generated copy constructor,
> } // not template instantiation
>
> ---
> [ 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 ]
>
>
Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ 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 ]