当可以在周围的宏参数的括号可以省略? [英] When can the parentheses around arguments in macros be omitted?

查看:147
本文介绍了当可以在周围的宏参数的括号可以省略?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经常和我感觉有些绕宏定义参数的括号是多余的。这太不方便圆括号一切。如果我能保证的说法毋须括号,我可以省略括号?或者圆括号他们都极力推荐?

我想出了这个问题,当我写:

 的#define SWAP_REAL(A,B,温度){做双TEMP =一个; A = B; B =温度;}而(0)

我认为,如果一个参数出现在宏的L值,括号可以省略,因为这意味着争论似乎没有其他操作。

我的理由是:


  1. 分配符号的关联优先级比只是逗号高。

  2. 您不能混淆编译器以为你的说法是前pression在它一个逗号。例如, SWAP(A,B,B)将不跨$ P $成功PTED为

      {做临时加倍= A,B;的a,b = B; B =温度;}而(0)

    这可以通过编译。


我说得对不对?谁能给我一个反例?

在下面的例子中,

 的#define ADD(A,B)(A + =(b)条)

我觉得参数 A 不需要括号。而在这种特殊情况下,既不需要参数 B ,对吧?

@JaredPar:


 的#include<&stdio.h中GT;
   #定义ADD(A,B)(A + =(b)条)
   诠释主(){
      诠释一个= 0,B = 1;
      ADD(A,B,2);
      返回0;
   }


这不能成功地在我的VS2010编译。 错误C2143:语法错误:缺少之前')';


简而言之,你不需要有圆括号出现在参数作为在宏的左值,但我们强烈建议圆括号他们。

宏的规则:


  1. 请不要让宏副作用!

  2. 对于无副作用的宏,只是圆括号所有参数没有第二个想法!

  3. 至于有副作用的宏,不要再迷恋!圆括号他们!肌钙蛋白T


解决方案

这是不安全的,如果你使用任何运营商是在你的前pression不是最低的precedence省略。我相信这将是逗号。

A OP B ,其中一些运营商如 + * 等会始终绑定错的,如果 A 包含低$ p $的前pression pcedence像 1 || 2

请注意我的的声称这是在这种情况下是安全的。还有更多的创造性的方式来打破宏。顺便说一句,不要使用带有副作用摆在首位的宏。更一般地,不使用宏的功能。 (参见,例如,C ++有效)。

Often and often I felt some of the parentheses around arguments in macro definitions were redundant. It’s too inconvenient to parenthesize everything. If I can guarantee the argument needs not be parenthesized, can I omit the parentheses? Or is parenthesizing them all highly recommended?

I came up with this question when I wrote:

#define SWAP_REAL(a, b, temp) do{double temp = a; a = b; b= temp;}while(0)

I think that if an argument appears as an l-value in the macro, the parentheses can be omitted because that means the argument appears with no other operation.

My reasoning is:

  1. The associative priority of assignment symbols is merely higher than comma’s.
  2. You cannot confuse the compiler into thinking that your argument is an expression with a comma in it. For example, SWAP(a, b, b) will not be interpreted successfully as

    do{double temp = a, b; a, b = b; b= temp;}while(0)
    

    which can pass compilation.

Am I right? Can anyone give me a counter-example?

In the following example,

#define ADD(a, b) (a += (b))

I think the argument a needn’t be parenthesized. And in this specific case, neither needs the argument b, right?

@JaredPar:

   #include <stdio.h>
   #define ADD(a, b) (a += (b))
   int main(){
      int a = 0, b = 1;
      ADD(a; b, 2);
      return 0;
   }

This cannot be compiled successfully on my VS2010. Error C2143: syntax error : missing ')' before ';'


In a nutshell, you don’t need to parenthesize the arguments having appeared as an l-value in the macro, but you are highly recommended to parenthesize them all.

Rules of Macros:

  1. DO NOT make macros with side effects!
  2. As for macros with no side effect, just parenthesize all the arguments without second thought!
  3. As for macros with side effect, stop being obsessive! Parenthesize them all! TnT

解决方案

It's unsafe to omit if you use any operator that isn't lowest precedence in your expression. I believe that would be comma.

a op b where op is some operator like +, * etc. will always bind wrong if a contains an expression of low precedence like 1 || 2.

Note I'm not claiming it's safe in those cases. There are more creative ways to break macros. By the way, don't use macros with side effects in the first place. More generally, don't use macros as functions. (See, e.g., Effective C++).

这篇关于当可以在周围的宏参数的括号可以省略?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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