On May 11, 6:02 pm, Juha Nieminen <nos...@thanks.invalidwrote:
Keith Halligan wrote:
Yes it appears you are right, C++ does not allow a templated copy
constructor. So the default one is always used.
Btw, what's the reason for this?
I'm not sure, but I would guess that it is simply so that the
compiler doesn't have to start instantiating things just to
decide whether to generate an implicit declaration or not.
Note that the issue is far from simple. The presence of a
templated constructor will not prevent the compiler from
generating its copy constructor. But when copying actually
takes place, the compiler does overload resolution as usual.
Including type deduction, so an instantiation of the template
function may end up in the overload set, and even be chosen over
the compiler generated copy constructor. For example:
class C
{
public:
C() {}
template< typename T >
C( T& obj ) {
std::cout << "In template" << std::endl ;
}
} ;
int
main()
{
C aC ;
C another( aC ) ;
return 0 ;
}
Displays "In template", dispite the fact that the context is one
where one would normally expect a "copy constructor".
--
James Kanze (Gabi Software) email:
ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34