Is it possible to overload a template class?

I found that template method could be overloaded, can I do the same on template classes? If 2 template classes match a template class instantiation, we can use the parameter type in the constructor to deduce which one to use.

template <typename T>
class A{
  A(T){}
};

template <typename T>
class A{
  A(T*){}
};

int main(){
  A<int*> a((int*)0);
  A<int> a((int*)0);
  return 0;
}

Answers


No. This is not allowed. Instead class template can be specialized (including partial specialization). This pretty much achieves the effect of overloading (which is only for functions)

Note that template parameters can not be deduced from constructor arguments.

template<class T> struct X{
   void f(){}
};

template<class T> struct X<T*>{
   void f(){}
};

int main(){
   X<int> x;
   x.f();          // calls X<T>::f

   X<int *> xs;
   xs.f();         // calls X<T*>::f
}

Need Your Help

Mercury Quick Test Pro and Virtual machines: Works from one client machine but not another

vb.net automated-tests qtp

I have a virtual machine (VMware) with Mercury Quick Test Professional 9.2 installed. I have a script to test an application, written in VB.NET using the Infragistics library.

Implementing object mapping using RestKit in Objective-C

objective-c xcode4.2 restkit

I am having trouble mapping a JSON response to objects using RestKit and Objective-C.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.