Templated copy constructor

K

kalita

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
 
D

Dietmar Kuehl

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

Victor Bazarov

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
 
C

Chris Theis

Victor Bazarov said:
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
 
V

Victor Bazarov

Chris said:
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
 
C

Chris Theis

Victor Bazarov said:
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
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top