copy constructor doubt

T

toton

I have a class like
template<typename T>
class my_class{
public:
int x_;
public:
template<typename U>
my_class(const my_class<U>& other ) : x_(other.x_){}
};

Now this one is not copy ctor in general. However in cases like when T
and U are of same type, will it be a copy ctor, or still machine
generated one will be used?

like
my_class<int> a_class;
my_class<int> other(a_class); => here what I am finding is that auto
generated copy ctor is used.
 
K

Keith Halligan

I have a class like
template<typename T>
class my_class{
public:
int x_;
public:
template<typename U>
my_class(const my_class<U>& other ) : x_(other.x_){}

};

Now this one is not copy ctor in general. However in cases like when T
and U are of same type, will it be a copy ctor, or still machine
generated one will be used?

like
my_class<int> a_class;
my_class<int> other(a_class); => here what I am finding is that auto
generated copy ctor is used.

The copy constructor you've written takes a paramaterised object of
type U, and not of type T which the templated class is defined with.
The compiler sees "const my_class<U> as different from "const
my_class<T>" and so does not treat it as the overridden
copyconstructor, so it defaults to the builtin one.
 
R

Ron Natalie

toton said:
I have a class like
template<typename T>
class my_class{
public:
int x_;
public:
template<typename U>
my_class(const my_class<U>& other ) : x_(other.x_){}
};

Now this one is not copy ctor in general. However in cases like when T
and U are of same type, will it be a copy ctor, or still machine
generated one will be used?
The machine generated one is used. A template is NEVER the copy
constructor.
 
R

Ron Natalie

Keith said:
The copy constructor you've written takes a paramaterised object of
type U, and not of type T which the templated class is defined with.
The compiler sees "const my_class<U> as different from "const
my_class<T>" and so does not treat it as the overridden
copyconstructor, so it defaults to the builtin one.

Nonsense. U and T can be the same type and the compiler makes
no distinction.

The problem is that the rule says that the templated function
NO MATTER HOW IT IS DEFINED will never inhibit the implicitly
defined copy constructor. Since the implicitly defined one
is there, it keeps the template from even being considered.
 
K

Keith Halligan

Nonsense. U and T can be the same type and the compiler makes
no distinction.

The problem is that the rule says that the templated function
NO MATTER HOW IT IS DEFINED will never inhibit the implicitly
defined copy constructor. Since the implicitly defined one
is there, it keeps the template from even being considered.

Yes it appears you are right, C++ does not allow a templated copy
constructor. So the default one is always used.
 
J

Juha Nieminen

Keith said:
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?
 
R

Ron Natalie

Juha said:
Btw, what's the reason for this?

I already answered this. Templates to not inhibit the
implicitly generated copy constructor. There's a
chicken and egg thing here as you've got two conditional
things: the implicit definition vs. whether the template
should be expanded. It is resolved towards the former.
 
J

James Kanze

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,786
Messages
2,569,625
Members
45,322
Latest member
ClaritaMcI

Latest Threads

Top