473,770 Members | 6,133 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Templated copy constructor

template<class T>
class A
{
template<class Y>
A(const A<Y> &)
{
// whatever
}
};

void f()
{
A<int> a1;
A<int> a2(a1); // here
}

Will my "templated copy constructor" be invoked?

cheers,
Marcin Kalicinski

Jul 23 '05 #1
5 4244
ka****@poczta.o net.pl wrote:
Will my "templated copy constructor" be invoked?


No. A template is never considered to be a copy constructor,
even if its signature might match that of a copy constructor.
You need to provide a separate copy constructor.
--
<mailto:di***** ******@yahoo.co m> <http://www.dietmar-kuehl.de/>
<http://www.contendix.c om> - Software Development & Consulting

Jul 23 '05 #2
ka****@poczta.o net.pl wrote:
template<class T>
class A
{
template<class Y>
A(const A<Y> &)
{
// whatever
}
};

void f()
{
A<int> a1;
A<int> a2(a1); // here
}

Will my "templated copy constructor" be invoked?


No. Since a1 and a2 are the same type, the non-template copy-c-tor
(generated by the compiler) will be used to create 'a2'.

V
Jul 23 '05 #3

"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:lk******** ***********@new sread1.mlpsca01 .us.to.verio.ne t...
ka****@poczta.o net.pl wrote:
template<class T>
class A
{
template<class Y>
A(const A<Y> &)
{
// whatever
}
};

void f()
{
A<int> a1;
A<int> a2(a1); // here
}

Will my "templated copy constructor" be invoked?


No. Since a1 and a2 are the same type, the non-template copy-c-tor
(generated by the compiler) will be used to create 'a2'.

V


Hmm, I thought there is no such thing like a "template copy ctor",
regardless of a probably matching signature. But I might be wrong. Could you
shed some light on this dark corner in my knowledge, please?

Cheers
Chris
Jul 23 '05 #4
Chris Theis wrote:
"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:lk******** ***********@new sread1.mlpsca01 .us.to.verio.ne t...
ka****@poczta .onet.pl wrote:
template<cla ss T>
class A
{
template<class Y>
A(const A<Y> &)
{
// whatever
}
};

void f()
{
A<int> a1;
A<int> a2(a1); // here
}

Will my "templated copy constructor" be invoked?


No. Since a1 and a2 are the same type, the non-template copy-c-tor
(generated by the compiler) will be used to create 'a2'.

V

Hmm, I thought there is no such thing like a "template copy ctor",
regardless of a probably matching signature. But I might be wrong. Could you
shed some light on this dark corner in my knowledge, please?


You are correct, there is no such thing as "template copy ctor". That's
why that expression was put in double quotes. Why the sarcasm? Do you
not understand the implied sense when things are in quotes? Then I can
explain. When something is put in quotes, it means that the 'as if' or
'like' rule is used. You know, right now I can write that I am answering
your "question". Get it? "Question". Implying that you did not actually
ask one, seemingly intending only to make a sarcastic remark...

V
Jul 23 '05 #5

"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:jO******** **********@news read1.mlpsca01. us.to.verio.net ...
Chris Theis wrote:
"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:lk******** ***********@new sread1.mlpsca01 .us.to.verio.ne t...
ka****@poczta .onet.pl wrote:

template<cla ss T>
class A
{
template<class Y>
A(const A<Y> &)
{
// whatever
}
};

void f()
{
A<int> a1;
A<int> a2(a1); // here
}

Will my "templated copy constructor" be invoked?

No. Since a1 and a2 are the same type, the non-template copy-c-tor
(generated by the compiler) will be used to create 'a2'.

V

Hmm, I thought there is no such thing like a "template copy ctor",
regardless of a probably matching signature. But I might be wrong. Could you shed some light on this dark corner in my knowledge, please?


You are correct, there is no such thing as "template copy ctor". That's
why that expression was put in double quotes. Why the sarcasm? Do you
not understand the implied sense when things are in quotes? Then I can
explain. When something is put in quotes, it means that the 'as if' or
'like' rule is used. You know, right now I can write that I am answering
your "question". Get it? "Question". Implying that you did not actually
ask one, seemingly intending only to make a sarcastic remark...

V


Hi Victor,

take a deep breath & relax ;-) There was no sarcasm intended on my side,
because I really wasn´t 100% sure about the templated copy ctor issue.
That´s why I asked - it was really meant as a question. Sorry, if it sounded
sarcastic because it certainly was not intended that way!

Cheers
Chris
Jul 23 '05 #6

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

Similar topics

1
1869
by: Rich | last post by:
Hi, I have a query regarding VC6 and its handling of templated copy constructors. Here goes: Take a look at the following code sample... template<class _Ty, size_t t_uiSize = 10 > class my_template
2
1918
by: ferdinand.stefanus | last post by:
Hi, I have some questions regarding templated class constructor: #include <iostream> using namespace std; template<typename T> class Foo { public:
6
1526
by: Dan Huantes | last post by:
I was presented a problem today where a class had member variable that was an object of a templated class. The class wanted to instantiate the object as a private member variable and call a constructor other than the default constructor. I couldn't figure out why it wasn't working so I changed the member variable to a pointer to an object instead and intialized it in the constructor and destroyed in the destructor. It bothered me that...
9
2198
by: shaun | last post by:
Dear all, I realized an error in a previous post, I reproduce it here because I'm still not sure how to solve it: I want to make a templated function which points to one-past-the-end of a simple array, to pass to a range constructor for a const vector. Here is some demonstration code: #include <iostream> using namespace std;
6
1581
by: Richard Thompson | last post by:
Hi - I have a program which was previously working (but wasn't well tested). I've added a new function call, and it now doesn't compile. The call requires a copy constructor, but the compiler appears to think that it actually calls one of the *other* constructors. The copy ctor would work in this context, but the other ctor can't be used (it leads to a template instantiation error, which the compiler is reporting).
7
1730
by: Claudius | last post by:
Hello, in my class TopTen I need to define three constructors while only the last one, the most general in terms of templates, should be sufficient in my opinion: template <typename Tnum, short Trank, bool Tcont> TopTen<Tnum,Trank,Tcont>::TopTen( const TopTen<Tnum,Trank,Tcont& tten );
3
12165
by: Lawrence Spector | last post by:
How does one call a templated constructor inside of a class when instantiating an object? I made up a quick sample to demonstrate. #include <iostream> class TestClass { public: template <class T> TestClass()
2
2290
card
by: card | last post by:
Hi everyone, I have a question about referencing a nested class contained within a templated class. Of course the best way to show you is by example. Here's my templated classes: #include <stack> template <class T> class A { public:
5
4024
Banfa
by: Banfa | last post by:
So I have a little problem, I have a template class and that class contains a template function; now what I want to do is declare that function in the class (or indeed the entire class) as a friend of all specialisations of the class. I did that (or thought I did) but the code I produced compiles with gcc 3.4.2 but not with Microsoft Visual Studio 2008 (Express Edition). So I have reduced my code to a minimum compilable example that exhibits...
0
9592
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10231
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
10059
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
10005
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
8887
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
7416
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
6679
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
5313
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
3972
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.