C++ template copy constructor on template class
06:24 12 Sep 2015

I have a template class that has a template copy constructor. The problem is when I instantiate this class using another instance of this class with the same template type, my template copy constructor is not called. Why doesn't it match?

Here is the code snippet:

#include 

template 
class MyTemplateClass
{
    public:
        MyTemplateClass()
        {
            std::cout << "default constructor" << std::endl;
        }

        /*
        MyTemplateClass(const MyTemplateClass& other)
        {
            std::cout << "copy constructor" << std::endl;
        }
        */

        template 
        MyTemplateClass(const MyTemplateClass& other)
        {
            std::cout << "template copy constructor" << std::endl;
        }
};

int main()
{
    MyTemplateClass instance;
    MyTemplateClass instance2(instance);
    return EXIT_SUCCESS;
}

The output is

default constructor

But if I explicitly write the default copy constructor (by uncommenting it), then the output becomes

default constructor
copy constructor

I really don't get it. I tested it with my local compiler (Clang 500.2.79) and with this one (GCC 4.9.2) and got the same result.

c++ templates copy-constructor