“#define FOO(template)”是什么意思?做?
我发现了一些奇怪的 C++ 预处理器子句,例如:
#define COMPILER_FLOAT_ENTRIES_DO(template)
将
#define COMPILER_FLOAT_ENTRIES_DO(template) \
template(jvm_fadd) \
template(jvm_fsub) \
template(jvm_f2d)
“template”保留字传递给 #define 并调用 template(something) 意味着什么?我在谷歌上找不到任何东西;可能是因为“#define”和“template”是非常常见的词。
I've found some weird C++ preprocessor clauses, like:
#define COMPILER_FLOAT_ENTRIES_DO(template)
and
#define COMPILER_FLOAT_ENTRIES_DO(template) \
template(jvm_fadd) \
template(jvm_fsub) \
template(jvm_f2d)
What does passing "template" reserved word to a #define, and calling template(something) mean? I couldn't find anything at Google; probably because "#define" and "template" are really common words.
The entire code is at https://phoneme.dev.java.net/source/browse/phoneme/components/cldc/trunk/src/vm/share/ROM/ROMWriter.hpp?rev=19839&view=markup.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
“模板”一词在这里是巧合。预处理器对这种方式的保留字不敏感,因为它主要只执行文本操作。
这是一个标准宏,它将函数名称作为其参数,并使用这些参数调用该函数三次。它的第一个版本没有任何内容,看起来像调试版本或其他一些旨在使其在某些上下文中成为无操作的版本。
The word "template" is a coincidence here. The preprocessor isn't sensitive to reserved words in this way, as it largely just does text operations.
That is a standard macro that takes a function name as its argument and calls that function three times with those arguments. The first version of it with nothing after looks like a debug version or some other version designed to make it a no-op in some context.
因此
COMPILER_FLOAT_ENTRIES_DO(x)
被替换为 ''。换句话说,它从代码中删除了该宏。因此,
COMPILER_FLOAT_ENTRIES_DO(x)
被替换为x(jvm_fadd) x(jvm_fsub) x(jvm_f2d)
。如果所有其他方法都失败,您可以使用
g++ -E -o foo.cpp
将宏预处理的输出保留在foo.cpp
中,以查看宏发生了什么。当然,您还需要通过命令行传入的所有其他编译标志(尤其是 -D 和 -I 标志)。so
COMPILER_FLOAT_ENTRIES_DO(x)
gets replaced with with ''. In other words, it removes that macro from the code.so
COMPILER_FLOAT_ENTRIES_DO(x)
gets replaced withx(jvm_fadd) x(jvm_fsub) x(jvm_f2d)
.If all else fails, you can see what is happening with macros by using
g++ -E -o foo.cpp
to leave the output of macro preprocessing infoo.cpp
. Of course you'll need all the other compilation flags passed in by command line as well (especially -D and -I flags).您经常在数据驱动编程设计模式中找到这种类型的编码。
当您想要多次包含同一文件(数据)但宏被不同的代码或其他数据替换时,这非常方便。
让我们假设一些属性和各自的类型:
编码部分可以使用数据,甚至不需要知道数量。
作为示例,让我们打印一些信息。
(请注意
#name
用于获取文本字符串“color”而不是值color
)这样您的代码< /strong> 独立于数据。
编辑:根据@RBerteig修正拼写错误和英文句子
You often find this type of coding in Data-Driven Programming design patterns.
This is handy when you want to include several times the same file (the Data) but where macros are substituted with different code or other data.
Let's assume some attributes and there respective types:
The coding part can use data without even needing to know the quantity.
As an example, let's print some information.
(note the
#name
is used to get the text string "color" instead of a valuecolor
)This way your code is independent from data.
Edit: fix typo and english sentence according to @RBerteig