[英]C++: complex function-like macro definition
While reading node.js source code, I came across a macro that I just can't understand. 在阅读node.js源代码时,我遇到了一个我无法理解的宏。
// Strings are per-isolate primitives but Environment proxies them
// for the sake of convenience.
#define PER_ISOLATE_STRING_PROPERTIES(V) \
V(address_string, "address") \
V(args_string, "args") \
V(argv_string, "argv") \
V(async, "async") \
V(async_queue_string, "_asyncQueue") \
V(atime_string, "atime") \
...
*I assume the variables (eg address_string) are defined in an included header file. *我假设变量(例如address_string)在包含的头文件中定义。
And it goes on like this for a while. 这样会持续一段时间。 I looked further down the code to see how it could be used.
我在代码中往下看,看如何使用它。
#define V(PropertyName, StringValue) \
inline v8::Local<v8::String> PropertyName() const;
PER_ISOLATE_STRING_PROPERTIES(V)
#undef V
From what I can understand, PER_ISOLATE_STRING_PROPERTIES(V) is a function-like macro that takes another function-like macro V as a parameter. 据我了解,PER_ISOLATE_STRING_PROPERTIES(V)是类函数宏,它将另一个类函数宏V作为参数。 I don't get the following:
我没有以下内容:
1- PER_ISOLATE_STRING_PROPERTIES(V) is given multiple definitions, and I don't get how these are used in the code (eg when PER_ISOLATE_STRING_PROPERTIES(V) is seen in code by the preprocessor, how does it know which definiton of V to replace it with?) 1- PER_ISOLATE_STRING_PROPERTIES(V)被赋予了多个定义,但我不知道如何在代码中使用它们(例如,当预处理器在代码中看到PER_ISOLATE_STRING_PROPERTIES(V)时,它如何知道用V的哪个定义来代替它)与?)
2- I don't get how the V function is used. 2-我不知道如何使用V函数。
Let's run ONLY the processor over the following code: 让我们仅在以下代码上运行处理器:
// Strings are per-isolate primitives but Environment proxies them
// for the sake of convenience.
#define PER_ISOLATE_STRING_PROPERTIES(V) \
V(address_string, "address") \
V(args_string, "args") \
V(argv_string, "argv") \
V(async, "async") \
#define V(PropertyName, StringValue) \
inline v8::Local<v8::String> PropertyName() const;
PER_ISOLATE_STRING_PROPERTIES(V)
#undef V
gcc - E code.cpp gcc-E code.cpp
This prints out: 打印输出:
inline v8::Local<v8::String> address_string() const; inline v8::Local<v8::String> args_string() const; inline v8::Local<v8::String> argv_string() const; inline v8::Local<v8::String> async() const;
C++ is a non-whitespace sensitive language, so it's essentially this: C ++是非空格敏感的语言,因此本质上是这样的:
inline v8::Local<v8::String> address_string() const;
inline v8::Local<v8::String> args_string() const;
inline v8::Local<v8::String> argv_string() const;
inline v8::Local<v8::String> async() const;
This technique is known as X Macros 该技术称为X宏
Personally, I don't see the harm in listing out all of the code that you can't easily put inside a for loop, but you can see how this technique is being used to avoid specifying the rest of the function signature again and again. 就我个人而言,我看不出列出所有您无法轻松放入for循环中的代码的危害,但是您可以看到如何使用这种技术来避免一次又一次地指定其余的函数签名。 Macros don't obey scope, and I'd much rather do a search and replace to make changes, then play with macros.
宏不遵循作用域,我宁愿进行搜索和替换以进行更改,然后使用宏。 Just the fact that you asked a question about this, proves that it hinders readability as well.
只是您问了一个问题,就证明它也阻碍了可读性。
tldr; tldr; The processor is being used to generate code.
处理器用于生成代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.