2009年1月19日 星期一

template partial specialization of member function

學到一招

如果有這樣一個東西
template < class T,unsigned N >
struct foo {
void bar();
};
假如我對某種類型的東西的foo有特別的需求, 可以這樣寫
template < > void foo < int,3 > ::bar() { ... }
但是如果我想要這樣
// 底下的東西不能編
template < unsigned N > void foo < double,n > ::bar(){ ... }
// 上面的東西是不行的

所以這時候只好這樣寫

template < > struct foo;

template < class T,unsigned N >
void _bar( foo< T,N > & a) { ... }

template < class T,unsigned N >
struct foo {
inline void bar() { _bar(*this); }
};

如果想要partial specialization 就這樣寫
template < unsigned N >
void _bar( foo < double,N > & a ) { ... }