带有变量的宏是否总是用变量或常量定义?
如果我在函数中有这样的宏:
void SomeFunc(int arg1, int arg2)
{
float value1, value2;
float range;
//#define MatrixBlock MyMatrix.block<arg1,arg2>(value1, value2)
//#define BlockRange MatrixBlock.block<arg2, range>
#define MatrixBlock MyMatrix.block(value1, value2, arg1, arg2)
#define BlockRange MatrixBlock.block(value1, value2, 0, range)
/* My code using the above macros */
// Are the following lines necessary? What will happen if I don't undefine the macro?
#undef MatrixBlock
#undef BlockRange
}
它每次都会获取新的 arg1 和 arg2 值还是会在第一次遇到宏时修复它们?我需要#undef
吗?如果我没有 #undef
会发生什么
If I have a macro like this in a function:
void SomeFunc(int arg1, int arg2)
{
float value1, value2;
float range;
//#define MatrixBlock MyMatrix.block<arg1,arg2>(value1, value2)
//#define BlockRange MatrixBlock.block<arg2, range>
#define MatrixBlock MyMatrix.block(value1, value2, arg1, arg2)
#define BlockRange MatrixBlock.block(value1, value2, 0, range)
/* My code using the above macros */
// Are the following lines necessary? What will happen if I don't undefine the macro?
#undef MatrixBlock
#undef BlockRange
}
Will it acquire new arg1 and arg2 values every time or will they be fixed the first time the macro is encountered? Do I need #undef
? What happens if I don't have the #undef
s
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
宏进行文本替换,基本上与在文本编辑器中进行搜索和替换相同。其结果被提供给 C 编译器来解析和生成代码。
该宏不关心
arg1
和arg2
是什么,它只是将字符串MatrixBlock
替换为字符串MyMatrix.block< arg1,arg2>(值1,值2)
。如何解释结果取决于编译器。Macros do textual replacement, basically the same as if you would so a search-and-replace in your text editor. The result of that is given to the C compiler to parse and generate code.
The macro doesn't care about what
arg1
andarg2
are, it just replaces the stringMatrixBlock
with the stringMyMatrix.block<arg1,arg2>(value1, value2)
. How the result is then interpreted is up to the compiler.宏只是文本替换。
在您的代码中,您已经定义了替换,但从未执行过它们。您需要执行以下操作:
所以是的,每次调用函数时您都会获得 arg1、arg2、value1、value2、range 的当前值。我注意到您正在尝试使用运行时值专门化一个模板,我认为这是行不通的。
如果您没有
undef
宏,那么它们将可供define
之后的所有代码使用,以便某些后续方法可以使用它们。如果它位于头文件中,那么包含它的任何内容都可以访问这些宏。在方法中定义的情况并不常见,但也并非闻所未闻。
A Macro is just a text substitution.
In your code you have defined the substitutions but never actioned them. You will need to do something like:
So yes, you will get the current values of arg1, arg2, value1, value2, range every time the function is called. I notice that you are trying to specialise a template with run time values which I don't think will work.
If you don't
undef
the macros then they will be available to all the code following thedefine
s so some subsequent method could make use of them. If this is in a header file then anything that includes it will have access to these macros.It is unusual to have defines within a method but not unheard of.
在编译器看到代码之前,宏将在单独的过程中作为文本替换进行处理。他们对函数和参数一无所知。
The macros are processed as text substitutions in a separate pass before the compiler sees the code. They know nothing about functions and parameters.