2013-05-04 13 views
2

是否有可能定義是這樣的:宏超載

#define FOO(x, y) BAR() 
#define FOO(x, sth, y) BAR(sth) 

讓這樣的:

FOO("daf", sfdas); 
FOO("fdsfs", something, 5); 

被翻譯成這樣:

BAR(); 
BAR(something); 

編輯: 其實,BAR是我的班的方法。對不起,以前沒有說過(沒想到它是相關的)。

回答DYP的問題:

class Testy 
{ 
    public: 
    void TestFunction(std::string one, std::string two, std::string three) 
    { 
     std::cout << one << two << three; 
    } 
    void AnotherOne(std::string one) 
    { 
     std::cout << one; 
    } 
    void AnotherOne(void) 
    { 
     std::cout << ""; 
    } 
}; 

#define PP_NARG(...) PP_NARG_(__VA_ARGS__,PP_RSEQ_N()) 
#define PP_NARG_(...) PP_ARG_N(__VA_ARGS__) 
#define PP_ARG_N(_1, _2, _3, N, ...) N 
#define PP_RSEQ_N() 3, 2, 1, 0 

// macro for exactly 2 arguments 
#define FOO_2(_1, _2) AnotherOne() 
// macro for exactly 3 arguments 
#define FOO_3(_1, _2, _3) AnotherOne(_2) 

// macro selection by number of arguments 
#define FOO_(N) FOO_##N 
#define FOO_EVAL(N) FOO_(N) 
#define TestFunction(...) FOO_EVAL(PP_NARG(__VA_ARGS__))(__VA_ARGS__) 

而且撥打:

Testy testy; 
testy.TestFunction("one", "two", "three"); // line 9 

編譯器輸出:

警告1個警告C4003:宏'PP_ARG_N的主要不夠的實際參數。 cpp 9

警告2警告C4003:不夠實際參數宏 'FOO_' 的main.cpp 9

錯誤3錯誤C2039: 'FOO_':不是 '暴躁' 的main.cpp 9

+2

爲什麼使用宏? – 0x499602D2 2013-05-04 13:26:16

+0

可以分別擴展到「BAR(sfdas)」和「BAR(something,5);」。這可以接受嗎?宏不能「放棄」最後一個參數,但可以放棄第一個參數。 – leemes 2013-05-04 13:29:49

+0

另請參閱http://stackoverflow.com/questions/3850421/can-i-define-variadic-c-preprocessor-macros-with-va-args-in-the-middle-instead – leemes 2013-05-04 13:31:36

回答

10

編輯部件 - - - - - - - - - - - - - - - - - - - - - - - - 看這裏 - -------------------------------------------------- ------------->

Overloading macro on number of arguments

// functions, or macros, .... 
void bar(){} 
void bar(int){} 

#define EXPAND(X) X // for MSVC10 compatibility 

// compute number of (variadic) macro arguments 
// from http://groups.google.com/group/comp.std.c/browse_thread/thread/77ee8c8f92e4a3fb/346fc464319b1ee5?pli=1 
#define PP_NARG(...) EXPAND(PP_NARG_(__VA_ARGS__, PP_RSEQ_N())) 
#define PP_NARG_(...) EXPAND(PP_ARG_N(__VA_ARGS__)) 
#define PP_ARG_N(_1, _2, _3, N, ...) N 
#define PP_RSEQ_N() 3, 2, 1, 0 


// macro for exactly 2 arguments 
#define FOO_2(_1, _2) bar() 
// macro for exactly 3 arguments 
#define FOO_3(_1, _2, _3) bar(_2) 

// macro selection by number of arguments 
#define FOO_(N) FOO_##N 
#define FOO_EVAL(N) FOO_(N) 
#define FOO(...) EXPAND(FOO_EVAL(EXPAND(PP_NARG(__VA_ARGS__)))(__VA_ARGS__)) 

int main() 
{ 
    int something = 42; 
    FOO("daf", sfdas); 
    FOO("fdsfs", something, 5); 
} 

預處理器輸出:

void bar(){} 
void bar(int){} 

int main() 
{ 
    int something = 42; 
    bar(); 
    bar(something); 
} 

EDIT2:好像VS2010與__VA_ARGS__和宏替換一些問題。

更新:這是一個錯誤? (也參見this SO question):

#define MACRO2(PARAM0, PARAM1, ...) arg 0: >PARAM0< arg 1: >PARAM1< \ 
    additional args: >__VA_ARGS__< 
#define MACRO1(...) MACRO2(__VA_ARGS__, OTHERARG_0, OTHERARG_1) 

MACRO1(ARG0, ARG1); 

預處理器輸出:

arg 0: >ARG0, ARG1< arg 1: >OTHERARG_0< additional args: >OTHERARG_1<; 

有關解決方法,查看鏈接SO問題。我已經更新了上面的原始答案(代碼),並使用MSVC10進行了測試 - >現在可以工作。

+0

我編輯了我的問題 - 'BAR'是一個類的方法,你的解決方案對此不起作用(編譯器說'FOO_PP_NARG'不是'MyClass'的成員)。 – NPS 2013-05-04 16:43:46

+0

@NPS這應該不重要。編譯器?確切的錯誤消息? – dyp 2013-05-04 17:29:06

+0

編譯器VS2010,其餘我在我的問題提供。 – NPS 2013-05-07 15:38:23