是否可以在函数中使用#define?

新手上路,请多包涵

例如,我看到如下源代码。我们可以在函数中使用 #define 吗?它是如何工作的? (更多信息:这段代码是我从 openvswitch 源代码复制的):

 void *
ofputil_put_action(enum ofputil_action_code code, struct ofpbuf *buf)
{
switch (code) {
case OFPUTIL_ACTION_INVALID:

#define OFPAT13_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) case OFPUTIL_##ENUM:
#include "ofp-util.def"
    OVS_NOT_REACHED();

#define OFPAT10_ACTION(ENUM, STRUCT, NAME)                  \
case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
#define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)      \
case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
#define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)        \
case OFPUTIL_##ENUM: return ofputil_put_##ENUM(buf);
#include "ofp-util.def"
}
OVS_NOT_REACHED();
}

#define OFPAT10_ACTION(ENUM, STRUCT, NAME)                        \
void                                                        \
ofputil_init_##ENUM(struct STRUCT *s)                       \
{                                                           \
    memset(s, 0, sizeof *s);                                \
    s->type = htons(ENUM);                                  \
    s->len = htons(sizeof *s);                              \
}                                                           \
                                                            \
struct STRUCT *                                             \
ofputil_put_##ENUM(struct ofpbuf *buf)                      \
{                                                           \
    struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s);   \
    ofputil_init_##ENUM(s);                                 \
    return s;                                               \
}
#define OFPAT11_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
OFPAT10_ACTION(ENUM, STRUCT, NAME)
#define OFPAT13_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
OFPAT10_ACTION(ENUM, STRUCT, NAME)
#define NXAST_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)            \
void                                                        \
ofputil_init_##ENUM(struct STRUCT *s)                       \
{                                                           \
    memset(s, 0, sizeof *s);                                \
    s->type = htons(OFPAT10_VENDOR);                        \
    s->len = htons(sizeof *s);                              \
    s->vendor = htonl(NX_VENDOR_ID);                        \
    s->subtype = htons(ENUM);                               \
}                                                           \
                                                            \
struct STRUCT *                                             \
ofputil_put_##ENUM(struct ofpbuf *buf)                      \
{                                                           \
    struct STRUCT *s = ofpbuf_put_uninit(buf, sizeof *s);   \
    ofputil_init_##ENUM(s);                                 \
    return s;                                               \
}
#include "ofp-util.def"

原文由 peng xu 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.6k
2 个回答

#define 是一个预处理器指令:它用于生成最终的 C++ 代码 ,然后再将其处理给将生成可执行文件的编译器。因此代码如下:

 for(int i = 0; i < 54; i++) {
  #define BUFFER_SIZE 1024
}

执行 54 次(在预处理器级别):预处理器只是在 for 循环上运行(不知道 for 循环是什么),看到一个定义语句,关联 1024BUFFER_SIZE 并继续。直到它到达文件的底部。

你可以在任何地方写 #define 因为预处理器并不真正了解程序本身。

原文由 Willem Van Onsem 发布,翻译遵循 CC BY-SA 3.0 许可协议

当然这是可能的。 #define 在编译器执行任何操作之前由预处理器处理。这是一个简单的文本替换。预处理器甚至不知道代码行是在函数、类或其他什么内部还是外部。

顺便说一句,在 C++ 中定义预处理器宏通常被认为是不好的风格。它们用于的大部分事情都可以通过模板更好地实现。

原文由 Frank Puffer 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题