Modern C++ Design中實現了一個自動生成類的方法。它用的是Loki中的TypeList。我在這里使用了boost MPL中的vector來作為類型的容器。按boost MPL庫的設計理念,其他的類型容器也應該可以利用這里的實現的(沒有試過,可能有問題,特別是map類型容器)
實現如下(注:我已將boost的頭文件放到了vc的include目錄中)
#include < boost/mpl/vector.hpp >
#include < boost/mpl/front.hpp >
#include < boost/mpl/pop_front.hpp >
template< typename Type >
struct Holder
{
Type value_;
};
template< typename TypeSequeue, template< typename > class Unit >
struct TypeConstract : public Unit< typename boost::mpl::front< TypeSequeue >::type >,
public TypeConstract< typename boost::mpl::pop_front< TypeSequeue >::type,
Unit >
{
};
template< template< typename > class Unit >
struct TypeConstract< boost::mpl::vector<>::type, Unit >
{
};
posted on 2007-04-07 16:02
walkspeed 閱讀(1750)
評論(1) 編輯 收藏 引用 所屬分類:
STL、Boost、范型編程 、
C++語言