简体   繁体   English

在C ++中混淆宏定义

[英]Confusing macro definition in C++

Recently,I am reading some code from B-Human and I ecountered this confusing snippet: 最近,我正在阅读B-Human的一些代码,我发现了这个令人困惑的片段:

void enumInit(char* enums, const char** names, int numOfEnums);

#define ENUM(Enum, ...) \
  enum Enum : unsigned char {__VA_ARGS__, numOf##Enum##s}; \
  inline static const char* getName(Enum e) \
  { \
    static char enums[] = #__VA_ARGS__; \
    static const char* names[numOf##Enum##s]; \
    static bool init = true; \
    if(init) \
    { \
      enumInit(enums, names, numOf##Enum##s); \
      init = false; \
    } \
    return e >= numOf##Enum##s ? 0 : names[e]; \
  }

I cannot understand how this macro works, how could it be a function definition in a macro definition? 我无法理解这个宏如何工作,它怎么可能是宏定义中的函数定义? It is a single cpp header file with #pragma once. 它是#pragma一次的单个cpp头文件。

void enumInit(char* enums, const char** names, int numOfEnums);

Isn't defined anywhere in the code you posted, so strictly speaking I don't know what it does. 未在您发布的代码中的任何位置定义,因此严格来说我不知道​​它的作用。

#define ENUM(Enum, ...) \
enum Enum : unsigned char {__VA_ARGS__, numOf##Enum##s}; \

This defines an enum with the values passed to the macro as "..." (google "variadic macro") as values, and an additional entry whose value is the number of elements in that macro and whose name is the concatenation of "numOf", the value of Enum (passed as a parameter) and "s". 这将枚举值传递给宏的值为“...”(google“variadic macro”)作为值,另外一个条目的值是该宏中元素的数量,其名称是“numOf”的串联“,Enum的值(作为参数传递)和”s“。 This last value is the entry with both value and position n if the enum contains n other values (starting at 0). 如果枚举包含n其他值(从0开始),则最后一个值是值和位置n的条目。

inline static const char* getName(Enum e) \
{ \
  static char enums[] = #__VA_ARGS__; \
  static const char* names[numOf##Enum##s]; \
  static bool init = true; \
  if(init) \
  { \
    enumInit(enums, names, numOf##Enum##s); \
    init = false; \
  } \
  return e >= numOf##Enum##s ? 0 : names[e]; \
}

This defines an inline function whereever your macro is called. 无论您的宏被调用,它都会定义内联函数。 It defines a static array containing the stringified value of the of the enum entries ( enums[] ), and their names ( names[...] ). 它定义了一个静态数组,其中包含枚举条目( enums[] )的字符串化值及其名称( names[...] )。 On the first fall, enumInit(...) is called. 在第一个秋天, enumInit(...) Likely, this function fills the names array with the names of the respective enum values. 可能,此函数使用相应枚举值的名称填充names数组。 For this, the string stored inside enums[] is used. 为此,使用存储在enums[]的字符串。 To know how exactly that works, I would need to know the definition of enumInit . 要知道它是如何工作的,我需要知道enumInit的定义。

edit: To more clearly answer the how could it be a function defination in a macro defination? 编辑:为了更清楚地回答如何在宏观定义中进行功能定义? part of your question: The macro itself is just pasted wherever it is used. 问题的一部分:宏本身只是粘贴在任何地方使用。 The function definition is inserted whereever it is called, so that one function is generated per macro call. 函数定义在被调用时插入,因此每个宏调用生成一个函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM