一个函数调用前(无效)使用 [英] Use of (void) before a function call
问题描述
可能重复:结果
铸造未使用的返回值作废
什么是目的(无效)
函数调用之前,例如:
What is the purpose of (void)
before a function call, for example
(void)func1();
我想这是一样简单地调用 func1的();
因此是(无效)
调用简单地让其他程序员都知道,返回类型将被忽略,例如,如果 func1的()
不得不 INT
,还是编译器可能在功能上进行一些优化?或许有它背后的另一个原因完全 - 它是合法的,甚至C ++或者是它一些遗留code看到的C遗迹。
Therefore is the (void)
call simply to let other programmers know that the return type will be ignored, for instance if func1()
had a return type of int
, or does the compiler perhaps perform some optimizations on the function? Perhaps there is another reason behind it altogether - is it even legal C++ or perhaps is it a remnant of C seen in some legacy code.
感谢
推荐答案
作废可以在一种情况下语义效应的投:其中一个值是逗号运算符的操作数和覆盖逗号操作符,强制转换为无效遗嘱SUP preSS是:
A cast to void can have semantic effect in one case: where a value is an operand of the comma operator and overrides the comma operator, a cast to void will suppress it:
struct S {
int operator,(int) { return 0; }
};
std::cout << (S(), 42) << '\n'; // prints '0'
std::cout << ((void) S(), 42) << '\n'; // prints '42'
这篇关于一个函数调用前(无效)使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!