Why cast unused return values to void?

≯℡__Kan透↙ 提交于 2019-11-25 23:30:26

问题


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?

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...


回答1:


David's answer pretty 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:

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.



回答2:


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:

static_cast<void>(fn());

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




回答3:


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

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




回答4:


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

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

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

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.




回答5:


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...




回答6:


I know this is an old thread, but the language evolves and it should be mentioned here for completeness that since c++17 we have the [[maybe_unused]] attribute which can be used instead of the void cast.




回答7:


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




回答8:


In C, it is perfectly OK to cast to void. Virtually anyone will understand the intent of the statement.

In C++, you have other tools at your disposal. Since C casts are usually frowned upon, and since the explicit cast to void will likely surprise your coworkers (it surprises mine), I have this function template somewhere

template <typename T>
void use_expression(const T&) {}

and I use

...
use_expression(foo());

where I would write (void)foo() in C.




回答9:


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)



来源:https://stackoverflow.com/questions/689677/why-cast-unused-return-values-to-void

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!