Re: Template conversion and default copy constructor
Jiang wrote:
Earl Purple wrote:
Stephan Tolksdorf wrote:
All the compilers I tested produced from the following code
executables that printed "2". Shouldn't the implicitly
defined copy constructor be called?
--
#include <iostream>
class Test{
public:
Test() : data(0) {}
template <typename T> Test(T& test) : data(2) { }
int data;
};
int main(int argc, char* argv[]) {
Test t1;
t1.data = 3;
Test t2(t1);
std::cout << t2.data;
}
--
No, the default copy-constructor is a closer match (being an exact
match) than the templated one and is called instead.
Not true.
In 12.8.p3, the standard says that a member function template
is never instantiated to perform the copy of a class object to an
object of its class type.
So overload resolution is totally irrelative here in my mind.
The passage you cite could be better worded, but it in this
case, we are not "performing a copy", in the sense of the
standard. We are initializing an object with an expression.
The fact that (co?ncidentally) the expression happens to have
the same type is irrelevant here.
Curiously enough (and don't ask me why), the fact that the
expression has the same type in:
Test t2 = t1 ;
means that we are NOT performing a copy, whereas if it had a
different type, a copy would be required.
--
James Kanze kanze.james@neuf.fr
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
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]