Here\'s the definition of copy constructor, [class.copy.ctor/1]:
A non-template constructor for class X is a copy constructor if its first parameter is of
Why is the "non-template" requirement there in the text?
Given it were different and copy constructors could be templates. How could a non-copy constructor not be ambiguous in the presence of a copy constructor template? Consider this:
struct Foo {
// ctor template: clearly useful and necessary
template
Foo(const T&) {}
// copy ctor: same signature! can't work
template
Foo(const T &) {}
};
Besides, constructing a Foo
from an object that is not a Foo
can be achieved by either conversion or ordinary construction, but allowing for copy-construction from a non-Foo
object changes the notion of copying to copying including conversion. But this can already be implemented with the existing scheme (conversion or non-copy construction).
In this example, here will be printed. So it seems that my template constructor is a copy constructor
The example that you show doesn't invoke copy construction, but an ordinary, implicit construction. If you change the constructor template to
template
Foo(const T &) {
// ^^^^^
printf("here\n");
}
then Foo b = a;
results in the compiler-generated copy constructor being called. Note that the copy ctor generated by the compiler has this signature:
Foo(const Foo&);
This requires adding a const
-qualifier to a
in Foo b = a;
. The original constructor template Foo(T&)
in your snippet is a better match, as no const
-qualifier is added.