超载C宏 [英] Overload C macros

查看:132
本文介绍了超载C宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有更好的方式来超负荷像这样的宏?我需要一个接受的参数不同数量的宏。

 的#define DEBUG_TRACE_1(P1)的std ::串P [] = {P1};登录_log(__ FUNCTION__,P,1)
的#define DEBUG_TRACE_2(P1,P2)的std ::串P [] = {P1,P2};登录_log(__ FUNCTION__,P,2)
的#define DEBUG_TRACE_3(P1,P2,P3)的std ::串P [] = {P1,P2,P3};登录_log(__ FUNCTION__,P,3)
的#define DEBUG_TRACE_4(P1,P2,P3,P4)的std ::串P [] = {P1,P2,P3,P4};登录_log(__ FUNCTION__,P,4)
的#define DEBUG_TRACE_5(P1,P2,P3,P4,P5)的std ::串P [] = {P1,P2,P3,P4,P5};登录_log(__ FUNCTION__,P,5)

这样调用

  DEBUG_TRACE_2(多,PARAMS);


解决方案

做你的具体的例子是一个可变参数宏最简单的方法:

 的#define DEBUG_TRACE(...)\\
    做{\\
        性病::串P [] = {} __VA_ARGS__; \\
        日志_log(__ FUNCTION__,P,(sizeof的P)/(sizeof的P [0])); \\
    }而(0)

一对夫妇的注意事项:


  1. __ __ VA_ARGS 是供应给宏观
  2. 逗号分隔的参数列表中的名称
  3. 您可以了解有多少使用您的情况的sizeof 因为p是一个静态数组

  4. 围绕do..while宏code通常被认为是很好的做法,因为它给变量(P)块范围,以便用户仍然可以拥有与宏外同名的变量,而(0 )部分很好地接受一个分号后没有打破一行if语句

如果你需要比这更多的灵活性,你可以用很巧妙的方法,让你明确将超载的宏观表现完全不同用不同数量的参数。然而,这使得code更加错综复杂,如果它是绝对必要的,才应使用。因为它看起来像可变参数会做得很好为您的使用情况下,我只是提供一个链接:
<一href=\"http://cplusplus.co.il/2010/07/17/variadic-macro-to-count-number-of-arguments/\">http://cplusplus.co.il/2010/07/17/variadic-macro-to-count-number-of-arguments/

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");

解决方案

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://cplusplus.co.il/2010/07/17/variadic-macro-to-count-number-of-arguments/

这篇关于超载C宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆