Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
158 views
in Technique[技术] by (71.8m points)

c++ - Overload C macros

Is there a better way to "overload" a macro like this? I need a macro that accepts various numbers of parameters.

#define DEBUG_TRACE_1(p1) std::string p[] = {p1}; log _log(__FUNCTION__, p, 1)
#define DEBUG_TRACE_2(p1, p2) std::string p[] = {p1, p2}; log _log(__FUNCTION__, p, 2)
#define DEBUG_TRACE_3(p1, p2, p3) std::string p[] = {p1, p2, p3}; log _log(__FUNCTION__, p, 3)
#define DEBUG_TRACE_4(p1, p2, p3, p4) std::string p[] = {p1, p2, p3, p4}; log _log(__FUNCTION__, p, 4)
#define DEBUG_TRACE_5(p1, p2, p3, p4, p5) std::string p[] = {p1, p2, p3, p4, p5}; log _log(__FUNCTION__, p, 5)

Called like this

DEBUG_TRACE_2("more", "params");
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The easiest way to do your specific example would be with a variadic macro:

#define DEBUG_TRACE(...)                                        
    do {                                                        
        std::string p[] = { __VA_ARGS__ };                      
        log _log(__FUNCTION__, p, (sizeof p) / (sizeof p[0]));  
    } while (0)

A couple notes:

  1. __VA_ARGS__ is the name for the list of comma-separated arguments supplied to the macro
  2. You can find out how many there are in your case using sizeof since p is a static array
  3. Surrounding your macro code in do..while is often considered good practice because it gives variables (p) a block scope so users can still have a variable with the same name outside the macro, and the while (0) portion nicely accepts a semicolon afterwards without breaking one line if statements

If you need more flexibility than this, you can use a very neat trick to allow you to explicitly "overload" the macro to behave completely differently with a different number of parameters. However, this makes the code much more convoluted and should only be used if it is absolutely necessary. Since it seems like variadic arguments will do fine for your use case, I'll just provide a link: http://efesx.com/2010/07/17/variadic-macro-to-count-number-of-arguments/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...