// // encoding: Shift_JIS // works on C++ template mechanism // #include //-------------------- // 定義:[リスト] // 整数定数 top と、 // リストである型 rest をメンバに持つ型のこと。 //-------------------- // // 先頭の整数と残りのリストを受けてリストを返す関数 // template struct listdef { static const int top = N; typedef Lst rest; }; // // リストの各要素にNを掛けたリストを返す関数 // template struct mult : listdef< Lst::top*N, mult > {}; // // 昇順のリストAとリストBを昇順にマージ // template struct merge; template struct merge_impl : merge {}; template struct merge_impl : listdef< LstB::top, merge > {}; template struct merge_impl : listdef< LstA::top, merge > {}; template struct merge { static const bool Same = (LstA::top == LstB::top); static const bool aFirst = (LstA::top < LstB::top); typedef merge_impl impl; static const int top = impl::top; typedef typename impl::rest rest; }; // // リストの先頭N要素を出力する。 // template struct print { static void doit() { std::cout << Lst::top << std::endl; print::doit(); } }; template struct print<0,Lst> { static void doit() {} }; // // テスト // ・1を含む。 // ・nを含むなら2nも3nも5nも含む。 // ・昇順 // を満たすリストxの定義 // struct x : listdef<1, merge< merge< mult<2,x>,mult<3,x> >, mult<5,x> > > {}; int main() { print<20, x>::doit(); }