带有变量的宏是否总是用变量或常量定义?

发布于 2024-11-03 18:53:04 字数 672 浏览 1 评论 0原文

如果我在函数中有这样的宏:

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 #undefs

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迷荒 2024-11-10 18:53:04

宏进行文本替换,基本上与在文本编辑器中进行搜索和替换相同。其结果被提供给 C 编译器来解析和生成代码。

该宏不关心 arg1arg2 是什么,它只是将字符串 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 and arg2 are, it just replaces the string MatrixBlock with the string MyMatrix.block<arg1,arg2>(value1, value2). How the result is then interpreted is up to the compiler.

泛泛之交 2024-11-10 18:53:04

宏只是文本替换。

在您的代码中,您已经定义了替换,但从未执行过它们。您需要执行以下操作:

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>      

    MatrixBlock; // as if you had written MyMatrix.block<arg1,arg2>(value1, value2); in the code
    BlockRange myRange; // as if you had written MatrixBlock.block<arg2, range> myRange; in the code
/* 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、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:

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>      

    MatrixBlock; // as if you had written MyMatrix.block<arg1,arg2>(value1, value2); in the code
    BlockRange myRange; // as if you had written MatrixBlock.block<arg2, range> myRange; in the code
/* 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 
} 

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 the defines 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.

回忆那么伤 2024-11-10 18:53:04

在编译器看到代码之前,宏将在单独的过程中作为文本替换进行处理。他们对函数和参数一无所知。

The macros are processed as text substitutions in a separate pass before the compiler sees the code. They know nothing about functions and parameters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文