如何编写一个模板,该模板将作为参数类,其构造函数具有互斥的签名?具有各种构造函数签名的类的模板
class A
{
A(){};
public:
int a;
A(int i) : a(i) {};
};
class B{
B(){};
public:
int a,b;
B(int i,int j) : a(i), b(j) {};
};
template <class T> class C {
public:
T* _t;
C(int i[])
{ //???
_t=new T(i[0]); //if T has T(int) signature
_t=new T(i[0],i[1]); //if T has T(int,int) signature
}
~C() {delete _t;}
};
int main()
{
int Ai[]={1,2};
C<A> _c(Ai); // template should work instantiated with A and B
C<B> _c(Ai); //
return 0;
}
的A
和B
的签名是固定的(不能被更改为int [例如])。上下文:我在考虑一个包装器,它将采用(专用)容器类型作为模板参数,例如T=vector<int>
或T=map<int,int>
,当需要调用构造函数时会出现问题。
谢谢。美丽的东西。 – 2012-04-06 20:26:06