0

I'm writing a template class for a dynamic list that allows you to insert three different types of data. I want to create three methods to insert an item within the list using the specializations. What is the right way to do this?

template <class T, class U, class V> class list 
{

.....

}

template <> list <class T> :: add (T item) {
   ...
   // insert elem type T
   ...
}

template <> list <class U> :: add (U item) {
   ...
   // insert elem type U
   ...    
}

template <> list <class V> :: add (V item) {
   ...
   // insert elem type V
   ...    
}
carlez
  • 93
  • 1
  • 1
  • 5

1 Answers1

1

You don't need to specialise at all. Just define your add functions as

void add(T item) {}
void add(U item) {}
void add(V item) {}

(from within the class).

Here's a matching example.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
Chowlett
  • 45,935
  • 20
  • 116
  • 150
  • You'll just need to be careful not to instantiate list with any two of T,U,V being the same type. – Asaf Sep 12 '13 at 10:21