Tuesday, May 1, 2012

Specialization of a single function


#include <iostream>



template<typename T>
struct A{
  void f();
};

template<typename T>
void A<T>::f(){
  std::cout<<"generic"<<std::endl;
}


template<>
void A<int>::f(){
  std::cout<<"int"<<std::endl;
}


int main(){
  A<int> x;
  x.f();
  A<double> d;
  d.f();
}

====output====

int
generic


No comments:

Post a Comment