简体   繁体   English

需要有关宏定义的帮助

[英]Need help regarding macro definition

Im reading c++ code, i have found such definition 我正在阅读c ++代码,我已经找到了这样的定义

#define USE_VAL(X) if (&X-1) {}

has anybody idea, what does it mean? 有什么想法,这是什么意思?

Based on the name, it looks like a way of getting rid of an "unused variable" warning. 根据名称,它看起来像是一种摆脱“未使用的变量”警告的方法。 The intended use is probably something like this: 预期用途可能是这样的:

int function(int i)
{
  USE_VAL(i)
  return 42;
}

Without this, you could get a compiler warning that the parameter i is unused inside the function. 如果没有这个,你可以得到一个编译器警告,在函数内部没有使用参数i

However, it's a rather dangerous way of going about this, because it introduces Undefined Behaviour into the code (pointer arithmetic beyond bounds of an actual array is Undefined by the standard). 然而,这是一种相当危险的方法,因为它在代码中引入了未定义的行为(超出实际数组边界的指针算法是标准的Undefined)。 It is possible to add 1 to an address of an object, but not subtract 1. Of course, with + 1 instead of - 1 , the compiler could then warn about "condition always true." 可以 1 添加到对象的地址,但不能减去1.当然,使用+ 1而不是- 1 ,编译器可以警告“条件始终为真”。 It's possible that the optimiser will remove the entire if and the code will remain valid, but optimisers are getting better at exploiting "undefined behaviour cannot happen," which could actually mess up the code quite unexpectedly. 优化器可能会删除整个if并且代码仍然有效,但优化器在利用“未定义的行为不可能发生”方面正在变得更好,这实际上可能会非常意外地搞乱代码。

Not to mention that fact that operator& could be overloaded for the type involved, potentially leading to undesired side effects. 更不用说operator&所涉及的类型可能过载的事实,可能导致不希望的副作用。

There are better ways of implementing such functionality, such as casting to void : 有更好的方法来实现这样的功能,例如转换为void

#define USE_VAL(X) static_cast<void>(X)

However, my personal preference is to comment out the name of the parameter in the function definition, like this: 但是,我个人的偏好是在函数定义中注释掉参数的名称,如下所示:

int function(int /*i*/)
{
  return 42;
}

The advantage of this is that it actually prevents you from accidentally using the parameter after passing it to the macro. 这样做的好处是它实际上可以防止您在将参数传递给宏后意外使用该参数。

Typically it's to avoid an "unused return value" warning. 通常,这是为了避免“未使用的返回值”警告。 Even if the usual "cast to void" idiom normally works for unused function parameters, gcc with -pedantic is particularly strict when ignoring the return values of functions such as fread (in general, functions marked with __attribute__((warn_unused_result)) ), so a "fake if " is often used to trick the compiler in thinking you are doing something with the return value. 即使通常的“cast to void”习惯通常适用于未使用的函数参数,当忽略fread等函数的返回值时, gcc with -pedantic也特别严格(通常,标记为__attribute__((warn_unused_result))函数),所以一个“假的if ”经常被用来欺骗编译器认为你正在使用返回值做某事。

A macro is a pre-processor directive, meaning that wherever it's used, it will be replaced by the relevant piece of code. 宏是一个预处理器指令,意味着无论在何处使用,它都将被相关的代码片段替换。

and here after USE_VAL(X) the space it is explain what will USE_VAL(X) do. 在USE_VAL(X)之后,它解释了USE_VAL(X)会做什么。

first it take the address of x and then subtract 1 from it. 首先它取x的地址然后从中减去1。 if it is 0 then do nothing. 如果是0那么什么也不做。

where USE_VAL(X) will used it will replaced by the if (&X-1) {} USE_VAL(X)将使用的地方将替换为if(&X-1){}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM