Topic: Are specializations of member templates allowed?


Author: Terence Kelling <kelling@arlut.utexas.edu>
Date: 1998/07/22
Raw View
I have a member template in a class foo that can take any type.  I want
to specialize it for the case when an int is passed and also for when a
class template of type bar is passed.  Can this be done?  Below is an
example of what I would like to do (I am unsure of the exact syntax).
Does the standard allow for this?

Thanks,
Terence Kelling

#include <iostream>
using namespace std;

template <class T>
class bar {

   public:
      T data;
};

class foo {

   public:
      template <class T> void func(T t);
};

// generic member template
template <class T>
void foo::func(T t) {

   cout << "generic func" << endl;
}

// specialization for double
template <>
void foo::func(double t) {

   cout << "specializtion for double" << endl;
}

// partial specialization for class bar
template <class U>
void foo::func(bar<U> t) {

   cout << "specializtion for class bar" << endl;
}



int main(void) {

   foo f;
   int i = 1;
   double d = 2.5;
   bar<int> b;

   f.func(i);
   f.func(d);
   f.func(b);

   return 0;
};

Output:
generic func
specialization for int
specialization for class bar



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]