C++ 为什么将未使用的返回值转换为 void?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/689677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:46:48  来源:igfitidea点击:

Why cast unused return values to void?

c++cvoid

提问by markh44

int fn();

void whatever()
{
    (void) fn();
}

Is there any reason for casting an unused return value to void, or am I right in thinking it's a complete waste of time?

是否有任何理由将未使用的返回值转换为 void,或者我认为这完全是在浪费时间?

Follow up:

跟进:

Well that seems pretty comprehensive. I suppose it's better than commenting an unused return value since self documenting code is better than comments. Personally, I'll turn these warnings off since it's unnecessary noise.

嗯,这似乎很全面。我认为这比注释未使用的返回值要好,因为自记录代码比注释更好。就个人而言,我会关闭这些警告,因为这是不必要的噪音。

I'll eat my words if a bug escapes because of it...

如果虫子因此逃脱,我会吃掉我的话......

采纳答案by Richard Corden

David's answerpretty much covers the motivation for this, to explicitly show other "developers" that you know this function returns but you're explicitly ignoring it.

大卫的回答几乎涵盖了这样做的动机,明确向其他“开发人员”展示您知道此函数返回但您明确忽略了它。

This is a way to ensure that where necessary error codes are always handled.

这是一种确保始终处理必要的错误代码的方法。

I think for C++ this is probably the only place that I prefer to use C-style casts too, since using the full static cast notation just feels like overkill here. Finally, if you're reviewing a coding standard or writing one, then it's also a good idea to explicitly state that calls to overloaded operators (not using function call notation) should be exempt from this too:

我认为对于 C++ 来说,这可能是唯一我更喜欢使用 C 样式转换的地方,因为在这里使用完整的静态转换符号感觉有点过分了。最后,如果您正在编码标准或编写一个编码标准,那么明确声明对重载运算符的调用(不使用函数调用表示法)也应免于此限制也是一个好主意:

class A {};
A operator+(A const &, A const &);

int main () {
  A a;
  a + a;                 // Not a problem
  (void)operator+(a,a);  // Using function call notation - so add the cast.

回答by David Holm

At work we use that to acknowledge that the function has a return value but the developer has asserted that it is safe to ignore it. Since you tagged the question as C++ you should be using static_cast:

在工作中,我们使用它来确认函数具有返回值,但开发人员断言忽略它是安全的。由于您将问题标记为 C++,因此您应该使用static_cast

static_cast<void>(fn());

As far as the compiler goes casting the return value to void has little meaning.

就编译器而言,将返回值强制转换为 void 没有什么意义。

回答by Daniel Earwicker

The true reason for doing this dates back to a tool used on C code, called lint.

这样做的真正原因可以追溯到用于 C 代码的工具,称为lint

It analyzes code looking for possible problems and issuing warnings and suggestions. If a function returned a value which was then not checked, lintwould warn in case this was accidental. To silence linton this warning, you cast the call to (void).

它分析代码以寻找可能的问题并发出警告和建议。如果一个函数返回了一个没有被检查的值,lint如果这是偶然的,它会发出警告。要lint对此警告保持沉默,您可以将调用转换为(void)

回答by Nawaz

Casting to voidis used to suppress compiler warnings for unused variables and unsaved return values or expressions.

强制转换void为用于抑制未使用的变量和未保存的返回值或表达式的编译器警告。

The Standard(2003) says in §5.2.9/4 says,

标准(2003)在 §5.2.9/4 中说,

Any expression can be explicitly converted to type “cv void.” The expression value is discarded.

任何表达式都可以显式转换为“cv void”类型。表达式值被丢弃

So you can write :

所以你可以写:

//suppressing unused variable warnings
static_cast<void>(unusedVar);
static_cast<const void>(unusedVar);
static_cast<volatile void>(unusedVar);

//suppressing return value warnings
static_cast<void>(fn());
static_cast<const void>(fn());
static_cast<volatile void>(fn());

//suppressing unsaved expressions
static_cast<void>(a + b * 10);
static_cast<const void>( x &&y || z);
static_cast<volatile void>( m | n + fn());

All forms are valid. I usually make it shorter as:

所有表格均有效。我通常将其缩短为:

//suppressing  expressions
(void)(unusedVar);
(void)(fn());
(void)(x &&y || z);

Its also okay.

它也没关系。

回答by florestan

Since c++17 we have the [[maybe_unused]]attribute which can be used instead of the voidcast.

从 c++17 开始,我们有了[[maybe_unused]]可以用来代替void强制转换的属性。

回答by Dani van der Meer

For the functionality of you program casting to void is meaningless. I would also argue that you should not use it to signal something to the person that is reading the code, as suggested in the answer by David. If you want to communicate something about your intentions, it is better to use a comment. Adding a cast like this will only look strange and raise questions about the possible reason. Just my opinion...

对于您将程序强制转换为 void 的功能而言,它是毫无意义的。我还认为您不应该使用它向正在阅读代码的人发出信号,正如大卫在回答中所建议的那样。如果您想传达一些有关您的意图的信息,最好使用评论。添加这样的演员表只会看起来很奇怪,并会引发有关可能原因的问题。只是我的观点...

回答by klew

Cast to void is costless. It is only information for compiler how to treat it.

铸造至虚空是无成本的。它只是编译器如何处理它的信息。

回答by Colin

Also when verifying your code complies to MISTA (or other) standards, automatic tools such as LDRA will not allow you to call a function that has a return type without having it return a value unless you explicitly cast the returned value to (void)

此外,在验证您的代码是否符合 MISTA(或其他)标准时,LDRA 等自动工具将不允许您调用具有返回类型的函数而不返回值,除非您将返回值显式转换为 (void)