Why Cast Unused Return Values to Void

Why cast unused return values to void?

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.

Why cast free's return value to void?

If we are talking about the standard free function then its prototype is

void free(void *ptr);

Therefore the cast is completely useless.

Now some speculation.

The author might have forgotten to include the stdlib.h header declaring this prototype, so the compiler is assuming the return type of it as int. Now during static analysis of this code the compiler was warning about the unused return value of what it thinks to be a non-void function. Such a warnings are usually silenced by adding the cast to void.

C - casting function call return values to void

This is not a functionality or anything. It is just an easy way to document or show the future coders or maintainers that the return value is ignored and the coder was aware of that. Nothing else except this.

Some function's (applying warn_unused_result attribute for gcc) enforce warning on ignoring implicitly the return value - this warning can be suppressed using void casting. (On some compiler versions (void)cast alone won't do - but that's different story)

Is casting a function return to void an old-style-cast or not?

It's fine.

(void) f(x);

is always equivalent to a static_cast as per [expr.static.cast]/6:

Any expression can be explicitly converted to type cv void, in which case it becomes a discarded-value expression.

Converting the result of a function to void is the way to make an expression a discard-value-expression. Now, the C++ way should be static_cast<void>(...)but (void) ... is an idiom (and is shorter).

Since the latter is well-defined and really common in codebases, gcc1 and clang2 made it not trigger Wold-style-cast.

It's well-defined, recognized by major compilers. It's fine.


1) g++ documentation --- 3.5 Options Controlling C++ Dialect

-Wold-style-cast (C++ and Objective-C++ only)

Warn if an old-style (C-style) cast to a non-void type is used within a C++ program. The new-style casts (dynamic_cast, static_cast, reinterpret_cast, and const_cast) are less vulnerable to unintended effects and much easier to search for.

2) not documented

Why cast an unused function parameter value to void?

It is there to avoid warnings from the compiler because some parameters are unused.

Why is the result of signal() cast to (void)?

This is to suppress the compiler warning about an unused return value (from the function call).

What does casting to `void` really do?

Casting to void is used to suppress compiler warnings. The Standard says in §5.2.9/4 says,

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

Purpose of casting a struct pointer to void without using the result

(void)bh; has no effect in the generated code. However, with many compilers it will prevent the compiler from issuing a warning about the parameter bh not being otherwise used in the function, and that was almost certainly the intent of the programmer. bh being a struct pointer is irrelevant; if this technique works at all, it will work regardless of the type of the unused parameter.

C++ has a more elegant way to accomplish this, allowing you to omit the variable name for any argument you don't need --

static unsigned
parent(const struct binheap *, unsigned u)
{
return (u / 2);
}

-- but C doesn't support this. Some compilers have extensions for the same purpose that are, if not so elegant, at least more self-describing, e.g. GCC's

static unsigned
parent(const struct binheap *bh __attribute__((unused)), unsigned u)
{
return (u / 2);
}

The (void)bh; trick is the only technique for suppressing unused-parameter warnings that uses only the facilities of ISO C; on the other hand, it's not guaranteed to work, the warnings a compiler emits are entirely up to that compiler, so you're already in implementation-defined territory no matter what you do.

C++ What is the purpose of casting to void?

Multiple purposes depending on what you cast

  • Marking your intention to the compiler that an expression that is entirely a no-op is intended as written (for inhibiting warnings, for example)
  • Marking your intention to to the compiler and programmer that the result of something is ignored (the result of a function call, for example)
  • In a function template, if a return type is given by a template parameter type T, and you return the result of some function call that could be different from T in some situation. An explicit cast to T could, in the void case, prevent a compile time error:

    int f() { return 0; } void g() { return (void)f(); }
  • Inhibiting the compiler to choose a comma operator overload ((void)a, b will never invoke an overloaded comma operator function).

Note that the Standard guarantees that there will never be an operator void() called if you cast a class object to void (some GCC versions ignore that rule, though).

Casting function returns to void

It explicitly means you ignore the return value, and did not just forget it.



Related Topics



Leave a reply



Submit