宏命名约定 [英] Macro naming convention

查看:141
本文介绍了宏命名约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想拥有它可以有可选参数的函数。当然这不能用C来完成,但也可以用一些宏魔:

的#define _macroWith1Arg(_0,_1,宏名,...)_ ##宏名称
#定义_macroWith2Args(_0,_1,_2,宏名,...)_ ##宏名称
#定义_macroWith3Args(_0,_1,_2,_3,宏名,...)_ ##宏名称#定义macroWith1Arg(宏名,...)_macroWith1Arg(_0,__VA_ARGS__,宏名## _1,宏名## _0)(__ VA_ARGS__)
#定义macroWith2Args(宏名,...)_macroWith2Args(_0,__VA_ARGS__,宏名## _2,宏名## _1,宏名## _0)(__ VA_ARGS__)
#定义macroWith3Args(宏名,...)_macroWith3Args(_0,__VA_ARGS__,宏名## _3,宏名## _2,宏名## _1,宏名## _0)(__ VA_ARGS__)#定义_sum_1(_1)(_1)
#定义_sum_2(_1,_2)(_1)+(_2)
#定义和(...)macroWith2Args(总和,__VA_ARGS__)fprintf中(标准错误,%D,SUM(1)); //输出1
fprintf中(标准错误,%D,和(1,2)); //打印3

我喜欢它的样子:它看起来像带有可选参数的正常功能。但是这样做的缺点是,你不能有一个函数指针宏象。所以,我听说你应该利用的名字,以纪念它明确作为一个宏(如SUM),以避免混淆。

现在我在我的问题是:是否真的有必要,我的意思是这个命名约定标准C与错误号例如(至少在我的平台),不是吗?我想大多数IDE可以识别宏和突出这些程序。我真的想听听你对这个意见。

修改

我找到了一种方法来实现,你可以采取一个函数指针:

INT my_sum(INT,INT);
#定义_sum_1(_1)my_sum(_1,0)
#定义_sum_2(_1,_2)my_sum(_1,_2)
#定义和(...)macroWith2Args(总和,__VA_ARGS__)
INT(*总和)(INT,INT)= my_sum;//使用它作为一个功能:
fprintf中(标准错误,%D,和(1,2)); //打印3
//将一个函数指针:
INT(* sumptr)(INT,INT)=总和;
fprintf中(标准错误,%D,sumptr(1,0)); //输出1


解决方案

在C(的preprocessor是它的一部分),比你在你的编辑建议你可以做更多:宏和函数可以有相同的名称。而这种情况很少应用code完成后,C库本身可以有。这里标准功能可以Implemeted一个为宏,但还必须提供具有相同名称的符号。

所以,标准本身并不坚持规则的宏名应全部大写。

我认为,如果你是在你认真落实,使


  • 调用带有全套参数宏观恰恰是在preprocessor阶段身份
  • 一个有效的函数声明带有相同的名称

那么就应该用小写标识没有问题的。

由您确保


  • 重复原型的功能仍然是一个有效的操作

  • 利用函数指针可以毫无问题地完成

和在所有实施行为的,如果宏在那里的功能。

顺便说一句,你贴code是不符合标准的。开始以下划线Indentifiers在文件范围内保留。这不是惯例,但势在必行。

I want to have functions which can have optional arguments. Of course this cannot be done with C, but it is possible with some macro magic:

#define _macroWith1Arg(_0, _1, macroName, ...)          _ ## macroName
#define _macroWith2Args(_0, _1, _2, macroName, ...)     _ ## macroName
#define _macroWith3Args(_0, _1, _2, _3, macroName, ...) _ ## macroName

#define macroWith1Arg(macroName, ...)               _macroWith1Arg(_0, __VA_ARGS__, macroName ## _1, macroName ## _0)(__VA_ARGS__)
#define macroWith2Args(macroName, ...)              _macroWith2Args(_0, __VA_ARGS__, macroName ## _2, macroName ## _1, macroName ## _0)(__VA_ARGS__)
#define macroWith3Args(macroName, ...)              _macroWith3Args(_0, __VA_ARGS__, macroName ## _3, macroName ## _2, macroName ## _1, macroName ## _0)(__VA_ARGS__)

#define _sum_1(_1)      (_1)
#define _sum_2(_1, _2)  (_1) + (_2)
#define sum(...)        macroWith2Args(sum, __VA_ARGS__)

fprintf(stderr, "%d ", sum(1)); // Prints 1
fprintf(stderr, "%d ", sum(1, 2)); // Prints 3

I like the way it looks: it looks like a normal function with optional arguments. But the drawback of this is you can't have a function pointer to a macro like sum. So I heard you should capitalize the name to mark it explicit as a macro (like "SUM") to avoid confusion.

Now I my question is: is this naming convention really necessary, I mean standard C does it with errno for example (at least on my platform)? I think most IDEs can identify macros and highlight them as such. I would really like to hear your opinion on this.

EDIT:

I found a way to achieve that you can take a function pointer:

int my_sum(int, int);
#define _sum_1(_1)     my_sum(_1, 0)
#define _sum_2(_1, _2) my_sum(_1, _2)
#define sum(...)       macroWith2Args(sum, __VA_ARGS__)
int (*sum)(int, int) = my_sum;

// Use it as a "function":
fprintf(stderr, "%d ", sum(1, 2)); // Prints 3
// Take a function pointer:
int (*sumptr)(int, int) = sum;
fprintf(stderr, "%d ", sumptr(1, 0)); // Prints 1

解决方案

In C (the preprocessor is part of it) you can do even more than you are suggesting in your edit: macros and functions can have the same name. Whereas this is rarely done in application code, the C library itself can have that. Here standard functions can be implemeted as macros, but a symbol with the same name must also be provided.

So the standard itself doesn't stick to the rule that macro names should be all caps.

I think that if you are careful in your implementation such that

  • calling the macro with a full set of parameters just is the identity in the preprocessor phase
  • a valid function declaration is provided with the same name

then there should be no problem in using a lowercase identifier.

By that you ensure that

  • repeating the prototype for your function is still a valid operation
  • taking a function pointer can be done without problems

and in all your implementation is behaving as if your macro where a function.

BTW, the code that you posted is not standard conforming. Indentifiers starting with an underscore are reserved in file scope. This is not a convention but imperative.

这篇关于宏命名约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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