Topic: templated Copy-Constructor?


Author: "Stephan Keil" <kei@parsytec.de>
Date: 2000/04/29
Raw View
Hi,

consider the following example:

 template<class T>
 struct Blah
 {
  Blah(){}

  template<class U>
  Blah(const Blah<U>&)
  {
   cout << "Blah" << endl;
  }
 };

 Blah<double> bd(Blah<int>());
 Blah<int> bi(Blah<int>());

With VC++ 6.0 the construction of 'bd' invokes the templated
constructor where as the construction of 'bi' does not
(i.e. apparently the compiler generated copy constructor
is used instead). Is this correct behaviour according to
the standard?

thanks for comments,

Stephan



---
[ 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: Ron Natalie <ron@sensor.com>
Date: 2000/04/29
Raw View

Stephan Keil wrote:
>

>   template<class U>
>   Blah(const Blah<U>&)
>   {
>    cout << "Blah" << endl;
>   }
>  };
>
>  Blah<double> bd(Blah<int>());
>  Blah<int> bi(Blah<int>());
>

You're not just defining a copy constructor above,
you're defining an constructor that takes an arbitrary type.
Is this what you want?  If you want to define a copy
constructor then :

 Blah(const Blah&) {
   ...
 }

would have been sufficient.

But getting back to your question.  No, this template member should not
be called.  A non-templated member (even an implicitly generated one)
will be preferred over a templated one.

---
[ 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: Roger Orr <roger_orr@my-deja.com>
Date: 2000/04/29
Raw View
In article <39099e33$0$22977@businessnews.de.uu.net>,
  "Stephan Keil" <kei@parsytec.de> wrote:
> Hi,
>
> consider the following example:
>
>  template<class T>
>  struct Blah
>  {
>   Blah(){}
>
>   template<class U>
>   Blah(const Blah<U>&)
>   {
>    cout << "Blah" << endl;
>   }
>  };
>
>  Blah<double> bd(Blah<int>());
>  Blah<int> bi(Blah<int>());
>
> With VC++ 6.0 the construction of 'bd' invokes the templated
> constructor where as the construction of 'bi' does not
> (i.e. apparently the compiler generated copy constructor
> is used instead). Is this correct behaviour according to
> the standard?
>
> thanks for comments,
>
> Stephan

Yes it is - see "12.8 Copying class objects" in the standard.
Basically a copy-constructor is defined as a non-templated method.

Roger.


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: "Michael Kochetkov" <mkochetk@trustworks.commm>
Date: 2000/04/29
Raw View
14.5.2/2 -- "A  normal  (non-template)  member  function   with  a given
name and type and a member function template of the same   name, which could
be used to generate a  specialization  of  the  same   type,  can  both be
declared in a class.  When both exist, a use of that name and type  refers
to the non-template member unless an explicit template  argument  list  is
supplied."

With regards,
Michael Kochetkov.

"Stephan Keil" <kei@parsytec.de> wrote in message
news:39099e33$0$22977@businessnews.de.uu.net...
> Hi,
>
> consider the following example:
>
>  template<class T>
>  struct Blah
>  {
>   Blah(){}
>
>   template<class U>
>   Blah(const Blah<U>&)
>   {
>    cout << "Blah" << endl;
>   }
>  };
>
>  Blah<double> bd(Blah<int>());
>  Blah<int> bi(Blah<int>());
>
> With VC++ 6.0 the construction of 'bd' invokes the templated
> constructor where as the construction of 'bi' does not
> (i.e. apparently the compiler generated copy constructor
> is used instead). Is this correct behaviour according to
> the standard?
>
> thanks for comments,
>
> Stephan
>
>
>
> ---
> [ 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              ]
>




---
[ 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: =?ISO-8859-1?Q?J=F6rg?= Barfurth <joerg.barfurth@attglobal.net>
Date: 2000/04/29
Raw View
Am 29.04.00, 02:41:36, schrieb "Stephan Keil" <kei@parsytec.de> zum Thema=
=20
templated Copy-Constructor?:


> Hi,

> consider the following example:

>  template<class T>
>  struct Blah
>  {
>   Blah(){}

>   template<class U>
>   Blah(const Blah<U>&)
>   {
>    cout << "Blah" << endl;
>   }
>  };

>  Blah<double> bd(Blah<int>());
>  Blah<int> bi(Blah<int>());

> With VC++ 6.0 the construction of 'bd' invokes the templated
> constructor where as the construction of 'bi' does not
> (i.e. apparently the compiler generated copy constructor
> is used instead). Is this correct behaviour according to
> the standard?

This is correct. The standard explicitly says that template constructors=20
(and assignment operators) wont be considered for copy construction (or=20
assignment).

Basically, the compiler-generated declarations are suppressed only if you=
=20
declare a non-template copy constructor (or assignment operator).

But then the (non-template) implicitly-declared overload takes precedence=
=20
over any matching template specializations.

Some more motivation: Otherwise the compiler, when seeing ...

template <class> struct Blah;
template <class T, class U> struct BlahDef;

template <class T>
struct Blah
{
 template <class U> Blah(Blah<U> const&);
 template <class U> Blah(U);

 template <class U> struct rebind { typedef Blah<U> other; }
 template <class U> Blah(typename rebind<U>::other const&);
 template <class U> Blah(typename Blah<U>::rebind<T>::other const&);

 template <class U> Blah(typename BlahDef<U, T>::type const&);
};

template <class T, class U> struct BlahDef
{=20
 typedef typename Blah<T>::rebind<U>::other type;
 // or simply: typedef T type;
};

... would have to try to find template arguments to instantiate a copy=20
constructor from the templates, in order to find out whether to=20
implicitly declare a copy constructor. Even restricting this to contexts=20
where the template arguments are deducible doesn't really improve the=20
situations.

As the standard is those template declarations might never be=20
instantiated.

> thanks for comments,

> Stephan

HTH

-- J=F6rg


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