2014-10-16 108 views
0

我有宏:如何獲取宏功能的名稱?

#define MYMACRO(pred, ...) \ 
    pred /* here 'pred' is 'mypred(1, 2)', but I need to get only the name without args */ 

這是用這樣的:

MYMACRO(mypred(1, 2)) 

我需要得到宏觀功能的只有名字,不帶參數。

我能做到這一點是這樣的:

#define mypred(...) \ 
    mypred 
#define MYMACRO_EXPAND(pred) \ 
    pred /* here 'pred' is 'mypred' without args */ 
#define MYMACRO(pred, ...) \ 
    MYMACRO_EXPAND(pred) 

但是這樣是不好的,因爲我要定義宏的所有可能的宏觀功能。 想法?

+5

這聽起來像一個XY問題。實際上你試圖解決什麼? – 2014-10-16 19:47:18

回答

0

你可以這樣做,但我不建議。 例如打印函數名稱。

#define MYMACRO(func, ...) \ 
    (printf("%s\n", #func), func(__VA_ARGS__)) 

而且使用這種方式

MYMACRO(mypred, 1, 2); 

int a = MYMACRO(pred, 1, 2);