473,326 Members | 2,108 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

copy constructor doubt

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<inta_class;
my_class<intother(a_class); =here what I am finding is that auto
generated copy ctor is used.

May 11 '07 #1
9 2592
On May 11, 10:59 am, toton <abirba...@gmail.comwrote:
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<inta_class;
my_class<intother(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<Uas different from "const
my_class<T>" and so does not treat it as the overridden
copyconstructor, so it defaults to the builtin one.

May 11 '07 #2
toton wrote:
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.
May 11 '07 #3
Keith Halligan wrote:
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<Uas 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.
May 11 '07 #4
On May 11, 1:21 pm, Ron Natalie <r...@spamcop.netwrote:
Keith Halligan wrote:
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<Uas 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.
Yes it appears you are right, C++ does not allow a templated copy
constructor. So the default one is always used.

May 11 '07 #5
On May 11, 5:19 pm, Ron Natalie <r...@spamcop.netwrote:
toton wrote:
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.
Thanks
I got the answer

May 11 '07 #6
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?
May 11 '07 #7
Juha Nieminen wrote:
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?
Consider asking about rationales in 'comp.std.c++'.
May 11 '07 #8
Juha Nieminen wrote:
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 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.
May 12 '07 #9
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

May 12 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
11
by: Sam Wilson [Bentley] | last post by:
If you pass a C++ object by value as an argument to a function which has a variable-length argument list (...), the MSVC7 C++ compiler does not call the object's copy constructor and will not...
3
by: sarathy | last post by:
Hi all, I have doubt regarding how objects are passed in C++. The primary problem of passing by value in C++, is that the destructor of the object passed will be called twice, thus creating...
8
by: shuisheng | last post by:
Dear All, I am wondering how the default copy constructor of a derived class looks like. Does it look like class B : public A { B(const B& right) : A(right) {}
7
by: sachinc.biradar | last post by:
Hi, I am pretty new to C++, I have following doubt. Why can't we pass an argument to constructor as pointer? As per my knowledge referance is like const pointer. Any help will be highly...
3
by: subramanian100in | last post by:
I thought the copy ctor always takes the form Test::Test(const Test & arg); for a class Test. But I read the following sentence in Stanley Lippman's C++ Primer 4th Edition(Page 476): The...
13
by: JD | last post by:
Hi, My associate has written a copy constructor for a class. Now I need to add an operator = to the class. Is there a way to do it without change her code (copy constructor) at all? Your help...
1
by: Rahul | last post by:
While reading "Efficient C++" by Dov Bulka I came across the follown statement "In addition, you must also define a copy constructor to "turn on" the Return Value Optimization(RVO). If the class...
2
by: sanjay | last post by:
Hi All, I have a doubt in understanding the output of the following program that i executed on my system. I was using DevC++ IDE which uses minGW based compiler. ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.