What Does Casting to 'Void' Really Do

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.

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

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

Casting to void (not pointer) is allowed, why?

Casting to void simply discards the result of the expression. Sometimes, you'll see people using this to avoid "unused variable" or "ignored return value" warnings.

In C++, you should probably write static_cast<void>(expr); rather than (void)expr;

This has the same effect of discarding the value, while making it clear what kind of conversion is being performed.

The standard says:

Any expression can be explicitly converted to type cv void, in which
case it becomes a discarded-value expression (Clause 5). [ Note:
however, if the value is in a temporary object (12.2), the destructor
for that object is not executed until the usual time, and the value of
the object is preserved for the purpose of executing the destructor.
—end note ]

ISO/IEC 14882:2011 5.2.9 par. 6

Is there a safe way to cast void* to class pointer in C++

Yes, static_cast is correct here.

Assuming that the void* value is only copied inside the C library, static_cast will return the original pointer value pointing to the passed object if you cast it to a pointer of the same type as it was originally.

Under some conditions you may also cast to a different type, the rules are those of reinterpret_cast (which simply casts via two static_casts with void* intermediate).

If you cast it to any type not allowed under those rules, e.g. an unrelated class type, trying to access the object through the pointer will cause undefined behavior. There is no way of detecting this mistake. You need to take care of doing it correctly yourself.

What is (void*) casting used for?

void*, usually referred to as a void pointer, is a generic pointer type that can point to an object of any type. Pointers to different types of objects are pretty much the same in memory and so you can use void pointers to avoid type checking, which would be useful when writing functions that handle multiple data types.

Void pointers are more useful with C than C++. You should normally avoid using void pointers and use function overloading or templates instead. Type checking is a good thing!

Type casting a variable to void?

This is to avoid unused variable warnings in some compilers.

Also there is a MISRA rule which states that all function parameters must be used. This method is a workaround for this rule.

What are the reasons for casting a void pointer?

You can cast a void* to another pointer in both languages. Perhaps you meant implicitly.

It's very convenient in C to not have to be explicit about it. In C++ we have templates, so to write generic code there's no need for void* casting and whatnot. In C there is no choice. A generic container has to hold void* to objects, and it's much easier to repeatedly say mydata* d = node; then it is mydata* d = (mydata*)node;.

So it's pretty much like you said. In C type safety in general didn't receive as much emphasis as it did in C++, especially when it came to void* because it was suppose to be a simple generic pointer to whatever. There's no need for that in C++, so better make it explicit when you're dealing with it.

cast to void**, for what reason?

Compilers will warn you these days unless you cast non-void pointers to void pointers when you pass them as void pointers (if that made any sense). I think you knew that, though.

The signature of void **lineptr is equivalent in C to void *lineptr[], which is the type of pointer that qsort takes as an argument. See this site for more details.



Related Topics



Leave a reply



Submit