More Related Content
Similar to 闇魔術を触ってみた (20)
闇魔術を触ってみた
- 9. BOOST_PP_CATを使ってみる
• とりあえずindex_tupleとindex_countが定義されてるとする
• これらについては本講座のスライド参照
#define INDEX_COUNT(from,to)¥
template<class>class BOOST_PP_CAT(index_count_help_, from); ¥
template<int... Nums>¥
class BOOST_PP_CAT(index_count_help_, from)¥
<index_tuple<Nums...>>{¥
public:¥
using type = from <Nums...>; }; ¥
template<size_t N>¥
using to = typename BOOST_PP_CAT(index_count_help_, from)¥
<typename index_count<N>::type>::type;
引数をfromとtoに代入して展開するマクロを定義
- 10. BOOST_PP_CATを使ってみる
• とりあえずindex_tupleとindex_countが定義されてるとする
• これらについては本講座のスライド参照
#define INDEX_COUNT(from,to)¥
template<class>class BOOST_PP_CAT(index_count_help_, from); ¥
template<int... Nums>¥
class BOOST_PP_CAT(index_count_help_, from)¥
<index_tuple<Nums...>>{¥
public:¥
using type = from <Nums...>; }; ¥
template<size_t N>¥
using to = typename BOOST_PP_CAT(index_count_help_, from)¥
<typename index_count<N>::type>::type;
トークン名結合してよくわからないクラスができる
- 11. BOOST_PP_CATを使ってみる
• とりあえずindex_tupleとindex_countが定義されてるとする
• これらについては本講座のスライド参照
#define INDEX_COUNT(from,to)¥
template<class>class BOOST_PP_CAT(index_count_help_, from); ¥
template<int... Nums>¥
class BOOST_PP_CAT(index_count_help_, from)¥
<index_tuple<Nums...>>{¥
public:¥
using type = from <Nums...>; }; ¥
template<size_t N>¥
using to = typename BOOST_PP_CAT(index_count_help_, from)¥
<typename index_count<N>::type>::type;
index_tupleで特殊化
- 12. BOOST_PP_CATを使ってみる
• とりあえずindex_tupleとindex_countが定義されてるとする
• これらについては本講座のスライド参照
#define INDEX_COUNT(from,to)¥
template<class>class BOOST_PP_CAT(index_count_help_, from); ¥
template<int... Nums>¥
class BOOST_PP_CAT(index_count_help_, from)¥
<index_tuple<Nums...>>{¥
public:¥
using type = from <Nums...>; }; ¥
template<size_t N>¥
using to = typename BOOST_PP_CAT(index_count_help_, from)¥
<typename index_count<N>::type>::type;
fromにNums…を渡したやつでtypeにエイリアス
- 13. BOOST_PP_CATを使ってみる
• とりあえずindex_tupleとindex_countが定義されてるとする
• これらについては本講座のスライド参照
#define INDEX_COUNT(from,to)¥
template<class>class BOOST_PP_CAT(index_count_help_, from); ¥
template<int... Nums>¥
class BOOST_PP_CAT(index_count_help_, from)¥
<index_tuple<Nums...>>{¥
public:¥
using type = from <Nums...>; }; ¥
template<size_t N>¥
using to = typename BOOST_PP_CAT(index_count_help_, from)¥
<typename index_count<N>::type>::type;
上のクラスにindex_countのtype渡してtoにエイリアス
- 14. さっきの例の使い方
template<int… Nums>class sum_type{
static int get(){
int ret();
for(const auto& x : {Nums…} )
ret+=x;
return ret;
}
};
INDEX_COUNT(sum,sum_type)
cout<<sum<10>::get()<<endl;//45が出力される
• つまりsum<10>でsum_type<0,1,2,3,4,5,6,7,8,9>に同じになる
• BOOST_PP_CATめっちゃ便利っぽい?
- 19. BOOST_PP_ENUM
• BOOST_PP_ENUM(count, macro, data)
• countに255以下の数字を入れる
• macroにはマクロを入れる
• macro(z,0,data),macro(z,1,data),…,macro(z,count-1,data)展開される
• zが謎すぎて謎だが見たところ2が渡されてるっぽい?
• 下手に触れないほうがいいっぽいです
- 30. FizzBuzzのための文字列
• FizzBuzzのルールを思い出す
• まず3の倍数であるかチェック
• 3の倍数なら「5の倍数ならFizzBuzz、そうでないならFizz」
• そうでないなら「5の倍数ならBuzz、そうでないならその数字」
• というわけでさっきのMOD_IF使う
• こんな感じ
#define FizzBuzzLoop(z,n,t)¥
MOD_IF(n,3,MOD_IF(n,5,FizzBuzz,Fizz),MOD_IF(n,5,Buzz,n))
• この引数の取り方はBOOST_PP_REPEAT_FROM_TOのため
- 35. 参考資料
• C++でゲームプログラミング http://d.hatena.ne.jp/osyo-manga/
• BOOST_PP http://d.hatena.ne.jp/osyo-manga/20100906/1285986310
• Boost.Preprocessorでプログラミングしましょう
http://www.slideshare.net/digitalghost/boostpp
• Boost.ppのリファレンス
http://www.boost.org/doc/libs/1_44_0/libs/preprocessor/doc/ref.html