在C中,当其他宏引发名称冲突时,如何使用其他宏定义宏 [英] In C, how to define a macro using other macros, when that other macros raise name conflicts

查看:319
本文介绍了在C中,当其他宏引发名称冲突时,如何使用其他宏定义宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是此之后的后续问题(请仔细阅读我的问题): 如何避免名称冲突来自#用C定义? (或C ++) 假设我使用#define定义ROW和COL.然后,我使用ROW和COL定义ARRSIZE.我声明一个像float myarray[ARRSIZE];这样的静态数组.当我修改ROW和COL时,静态数组大小会相应更改.但是在我的特殊情况下,名称ROW和COL名称与我在同一文件中使用的结构类型的成员名称冲突.有人告诉我使用const变量,而不要使用'#define'以避免冲突.我喜欢将代码修改为beloow(这是一个示例).

This is a follow-up question after this (please read my question to the end) : how to avoid name conflicts coming from #define in C? (or C++) Suppose I define ROW and COL using #define. I then define ARRSIZE using ROW and COL. I declare a static array like float myarray[ARRSIZE];. When I modify ROW and COL, the static array size changes accordingly. But in my special case, the names ROW and COL name-conflicts with a member name of a struct type I'm using in the same file. Someone told me to use const variable instead of using '#define' to avoid the confict. I liked that I modifed the code as beloow(it's an exmple).

const int ROW = 100;
const int COL = 200;
const int ARRSIZE = ROW*COL;
float myarray[ARRSIZE];

对此进行编译可以使我

error: expected '=', ',', ';', 'asm' or '__attribute__' before 'ARRSIZE'

在定义ARRSIZE的那一行

.当然,我可以在函数内部使用int ARRSIZE = ROW*COL;,并使用malloc在函数内部动态分配数组.但是,如果我只想更改ROW和COL而又不想使用malloc怎么办?当然,使用#define进行操作不会有问题(名称冲突除外).

at the line where I define ARRSIZE. Of course I can use int ARRSIZE = ROW*COL; inside a function and dynamically allocate the array inside the function using malloc. But what if I want to change only ROW and COL and don't want to use malloc? Of course there is no problem (except the name conflicts) when doing it with #define.

#define ROW 100
#define COL 200
#define ARRSIZE ROW*COL
float myarray[ARRSIZE];

所以问题是:我只想更改ROW,COL,并希望自动更改静态数组的大小,但是同时,ROW和COL在同一源文件中显示为类型(结构)的成员变量. ,而且我无法固定类型的成员名称(假设它来自系统提供的头文件).总之,我想定义一个不适用于成员函数/变量或全局变量的类似宏的值".在我的情况下,最佳做法是什么?只是将我的变量ROW和COL更改为特殊的东西?问了这个问题后,我得到的印象可能是唯一的解决方案.:)

So the problem : I want to change only ROW, COL and want the static array size automatically changed, but at the same time, ROW and COL appears as a member variable of a type(a struct) in the same source file, and I cannot fix member name of the type(suppose it's from a header file provided by the system). So in a word, I want to define a 'macro like values' that is not applied to a member function/variable or global variables. What is the best practice to do it in my case? just change my variable ROW and COL to something special? Having asked this question, I get an impression that may be the only solution.. :)

推荐答案

编译注释以给出答案:

解决方案A,
如果您由于某种原因不得不在代码中坚持使用"ROW"和"COL"(获得美尔康宁的积分):

Solution A,
if you for some reason have to stick with "ROW" and "COL" in your code (credits to melpomene):

enum { ROW = 100 }; 
enum { COL = 200 }; 
enum { ARRSIZE = ROW*COL };

解决方案B,
如果您可以自由选择标识符;为将来的重用提供更强大的功能:
通过选择不同的,非冲突的,更长的标识符来避免命名冲突.
我对简短而又明显的标识符不屑一顾,您遇到的冲突就是一个很好的例子.其他例子花费了我的老板很多时间和金钱.

Solution B,
if you are free to choose identifiers; more robust for future reuse:
Avoid the naming conflict by choosing different, non-conflicting, longer identfiers.
I have a superstitious distrust of short, obvious identifiers, the conflict you encountered is a good example why. Other examples have cost my employers quite some time and money.

(我发现其他地方的美洛美犬对答案的声誉不再感兴趣,非常无私.我认为这里值得做一个很好的Q/A配对,而从美洛美尼身上吸取任何东西.)

这篇关于在C中,当其他宏引发名称冲突时,如何使用其他宏定义宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆