473,804 Members | 3,138 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

copy constructor doubt

I have a class like
template<typena me T>
class my_class{
public:
int x_;
public:
template<typena me 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_c lass;
my_class<intoth er(a_class); =here what I am finding is that auto
generated copy ctor is used.

May 11 '07 #1
9 2609
On May 11, 10:59 am, toton <abirba...@gmai l.comwrote:
I have a class like
template<typena me T>
class my_class{
public:
int x_;
public:
template<typena me 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_c lass;
my_class<intoth er(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<typena me T>
class my_class{
public:
int x_;
public:
template<typena me 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.n etwrote:
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.n etwrote:
toton wrote:
I have a class like
template<typena me T>
class my_class{
public:
int x_;
public:
template<typena me 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*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
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
5814
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 kind. It sounds simple but evidently .NET has difficulty with this concept for some reason. I do understand that .NET objects are created on the GC heap but that doesn't mean that they couldn't be copied from another object of the same kind when...
11
2345
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 complain if the copy constructor is private. 1) Is this part of the C++ language definition? What is the thinking behind it? 2) In any case, how can I catch at compile time any callers that try to
3
1778
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 possible damage to the object passed. If that is the case, why isnt the following program producing the unexpected o/p. I expect the program to print In B::printValues : 22 33 instead of
8
4301
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
2109
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 appreciated. Regards, Sachin
3
361
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 copy constructor is a special constructor that has a single parameter that is a (usually const) reference to the class type.
13
3980
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 is much appreciated. JD
1
1902
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 involved does not have a copy constructor defined, the RVO is quietly turned off." I searched a lot on the groups but still could not find any satisfactory answer which explains this. My doubt is "Even if we don't define a copy constructor then...
2
2448
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. ---------------------------------------------- #include <iostream> using namespace std;
0
9579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10575
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10330
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10319
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9144
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7616
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6851
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5520
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4297
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.