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 ) { ... }

2 則留言:

匿名 提到...

This post offers clear idea in support of the new viewers of blogging, that actually how to
do blogging and site-building.
Feel free to visit my blog ... the truth about making money online

匿名 提到...

Spot on with this write-up, I actually believe this site
needs far more attention. I'll probably be returning to read through more, thanks for the advice!
My page: easy fast ways to make money