Copy constructor of a template class

c++

Solution

There are strict rules what constitutes a copy constructor (cf. C++11, 12.8):

It is not a template.

For a class `T`, its first argument must have type `T &` or `T const &` or `T volatile &` or `T const volatile &`.

If it has more than one argument, the further arguments must have default values.

If you do not declare a copy constructor, a copy constructor of the form `T::T(T const &)` is implicitly declared for you. (It may or may not actually be defined, and if it is defined it may be defined as deleted.)

(The usual overload resolution rules imply that you can have at most four copy constructors, one for each CV-qualification.)

There are analogous rules for move constructors, with `&&` in place of `&`.

Problem

I read that template copy-con is never the default copy constructor, and template assignment-op is never a copy assignment operator. I couldn't understand why this restriction is needed, and straight away went online to Ideone and return a test program. But here, the copy constructor never gets called, on further googling I came across templatized constructor and tried that, but still, it never calls the copy constructor. ``` #include <iostream> using namespace std; template <typename T> class tt { public : tt() { std::cout << std::endl << " CONSTRUCTOR" << std::endl; } template <typename U> const tt<T>& operator=(const tt<U>& that){std::cout << std::endl << " OPERATOR" << std::endl;} template <typename U> tt(const tt<U>& that) { std::cout << std::endl << " COPY CONSTRUCTOR" << std::endl; } }; tt<int> test(void) { std::cout << std::endl << " INSIDE " << std::endl; tt<int> a; return a; } int main() { // your code goes here tt<int> a ; a = test(); return 0; } ``` What's the reason behind putting this restriction? And also, how do I write a copy constructor of a template class.

Original source

Related problems