2017-12-30 47 views
-1

あるトークン、
で別のマクロ作品というトークンを連結が、トークンがマクロであるとき、それはしていないようです作業?GCCプリコンパイラマクロ##、<br> 、また、あなたが働くトークンを連結見ることができるように別のマクロ

longNameForaFunction_one(){return 1;} 
longNameForaFunction_two(){return 2;} 
longNameForaFunction_third(){return 3;} 
two(){return 2;} 
#define bar two 
#define foo(x)(longNameForaFunction_##x()) 
#define three third 
main(){ 
printf("%d\n",foo(one)); // 1 

printf("%d\n",foo(two)); // 2 

printf("%d\n",bar()); // 2 

// printf("%d\n",foo(three)); // this doesn't work 
} 

最後の行は、コメントを外すとこのエラーが発生します。

`longNameForaFunction_three」への未定義の参照が

#define three third 

は、あなたはそれが動作する前に別のレベルを提供する必要がある理由です何の効果

Try it online

+1

さらに空白を使用します。 '#define EVALUATOR(x)x'と' #define CONCATENATE(x、y)x ## y'を使い、 '#define foo(x)CONCATENATE(longNameForaFunction_、EVALUATOR(x))'を使ってください。そして、これは重複しています。 –

答えて

1

まあを持っていないようだ - マクロパラメータは、fooに渡される前に展開されます。

#define foo(x)(longNameForaFunction_##x()) 
#define foo1(x) foo(x) 
#define three third 

.. 

printf("%d\n",foo1(three)); 
関連する問題