やあ子供たち、元気にしてたかな、悪いものを食べ過ぎたり体調が悪かったりストレスがあったりして蕁麻疹なんかにはなってないかい?大丈夫かな。今日は任意キーワードをリテラルにしてしまうマクロ技の話だよ。
引数付きマクロの定義中では、引数の前に「#」をつけるだけでその変数をリテラルにしてくれるよ
#define TO_LITERAL(x) #x #define TO_LITERAL2(x) "It is " #x " who robbed the bicycle." int main (int argc, char * const argv[]) { cout << TO_LITERAL( "Tom" ) << endl; cout << TO_LITERAL2( Tom )<< endl; cout << TO_LITERAL2( Wendy )<< endl; cout << TO_LITERAL2( the same guy )<< endl; return 0; }
こんな書き方ができるんだね。これの出力は以下のようになったよ。
"Tom"
It is Tom who robbed the bicycle.
It is Wendy who robbed the bicycle.
It is the same guy who robbed the bicycle.
いろいろ試してみてくれ。じゃ!