Copy constructor 模板类内的模板复制构造函数

Copy constructor 模板类内的模板复制构造函数,copy-constructor,template-specialization,Copy Constructor,Template Specialization,我有一个template类,它有一些模板副本构造函数,这是很有用的。 但是我想专门化int的复制构造函数,但我做不到。 此程序打印:CC。但需要:集成电路 请帮帮我 #include <cstdio> #include <cstdlib> #include <iostream> template<class X> class complex { public: complex

我有一个template类,它有一些模板副本构造函数,这是很有用的。 但是我想专门化int的复制构造函数,但我做不到。 此程序打印:CC。但需要:集成电路 请帮帮我

    #include <cstdio>
    #include <cstdlib>
    #include <iostream>

    template<class X> 
    class complex
    {
    public:
       complex() {}
       template<class T> complex(const complex<T>& c) {std::cout << "2";}
       template<class T> complex(const complex<T*>& c) {std::cout << "1"; }
       complex(const complex<X>& c) {std::cout << "C";}

       template<int> complex(const complex<int>& c){std::cout << "i";}
    };

    int main(int argc, char *argv[])
    {

       complex<int> c1;
       complex<double> c2;

       complex<int> c3(c1); //2
       complex<double> c4(c2);

    }
对于X==int,就像在复杂c3c1中从另一个复杂初始化复杂时一样,非模板complexconst complex&c将被认为比模板专门化更匹配,因此这是按预期工作的


尝试使用复杂的c5c1,其中类型不匹配。在这种情况下,非模板构造函数(因为它需要一个复杂的参数)不会被选择为比您的专门化更好的构造函数。

我今天可以这样做,添加带有专门化的新类。模板类complex{public:complex{}template complexconst complex&c{std::cout