(4) 這里function_invoker是用的type-erase手法,具體可參見我以前寫的boost.any源碼剖析,或上篇講auto的,或《C++ Template Metaprogramming》(內有元編程慎入?。?。type-erase手法是像C++這樣的弱RTTI支持的語言中少數(shù)真正實用的手法,某種程度上設計模式里面的adapter模式也是type-erase的一個變種。
如果還覺得不夠的話,可以參考variadic-templates的主頁,上面的variadic templates proposal中帶了三個tr1實現(xiàn),分別是tuple,bind,function,當然,variadic-templates的好處遠遠不僅僅止于這三個實現(xiàn),從本質上它提供了一種真正直接的表達意圖的工具,完全避開了像下面這種horrible的workaround:
template
cons(T1& t1, const null_type&, const null_type&, const null_type&,
const null_type&, const null_type&, const null_type&,
const null_type&, const null_type&, const null_type&)
: head (t1) {}
tuple的C++98實現(xiàn),代碼近千行。利用variadic-templates實現(xiàn),代碼僅百行。
和這種更horrible的workaround:
template
_bi::bind_t::type>
BOOST_BIND(boost::type, F f, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6)
{
typedef typename _bi::list_av_6::type list_type;
return _bi::bind_t(f, list_type(a1, a2, a3, a4, a5, a6));
}
如果還覺得不夠的話,可以參考variadic-templates的主頁,上面的variadic templates proposal中帶了三個tr1實現(xiàn),分別是tuple,bind,function,當然,variadic-templates的好處遠遠不僅僅止于這三個實現(xiàn),從本質上它提供了一種真正直接的表達意圖的工具,完全避開了像下面這種horrible的workaround:
template
cons(T1& t1, const null_type&, const null_type&, const null_type&,
const null_type&, const null_type&, const null_type&,
const null_type&, const null_type&, const null_type&)
: head (t1) {}
tuple的C++98實現(xiàn),代碼近千行。利用variadic-templates實現(xiàn),代碼僅百行。
和這種更horrible的workaround:
template
_bi::bind_t::type>
BOOST_BIND(boost::type, F f, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5, A6 a6)
{
typedef typename _bi::list_av_6::type list_type;
return _bi::bind_t(f, list_type(a1, a2, a3, a4, a5, a6));
}