0

It looks like this code is acceptable with gcc even though the standard states that the value of the expression is discarded in case of a void cast.

#include <iostream>

int f(int i) {
    int a = i*2;
    (void)a;
    a++; // looks OK even after void cast
    return a; // looks OK even after void cast
}

int main() {
    std::cout << "f(5): " << f(5) << "\n"; // displays 11
    return f(5);
}

So is it generally possible to use a change the value of a variable that has been void casted, and then what does "discarded" means in this case? Or is it a bad practice to rely on it?

FlashMcQueen
  • 635
  • 3
  • 13
  • 1
    I think you can examine this link for your question; https://stackoverflow.com/questions/34288844/what-does-casting-to-void-really-do#:~:text=Casting%20to%20void%20is%20used,The%20expression%20value%20is%20discarded. – Ogün Birinci Feb 18 '21 at 10:10
  • You definitely misunderstood the standard on that part. – user202729 Feb 18 '21 at 10:10
  • The value of `a` is the result of the expression that is retrieved and then discarded. The original contents of `a` are unaffected. – Richard Critten Feb 18 '21 at 10:10
  • I think you're confusing the value of the variable with the variable itself. Casting has no efffect whatsoever on the cast's argument. – molbdnilo Feb 18 '21 at 10:16
  • Not really, (1) it might call `operator int` on the object, and (2) you can cast things to `char&` and assign to it (usually causes undefined behavior). – user202729 Feb 18 '21 at 10:19

1 Answers1

3

The result of the expression is discarded, which, in your case, means that the result of (void)a is discarded. This is usually used to avoid unused variable (or unused return value) warnings. It is perfectly fine to use a after (void)a, but there's no point to do the (void)a in the first place.

EDIT: To address some concerns raised in the comments. The exact wording of the standard is:

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

In this case a is the expression and it is casted to void which discards the result of the expression, so "the result of (void)a" isn't technically speaking correct: the result of a is discarded because it is casted to void - but one can also consider that the result of the cast itself is discarded (in essence, the cast does nothing).

icebp
  • 1,608
  • 1
  • 14
  • 24
  • *It means that the result of `a` is discarded. – user202729 Feb 18 '21 at 10:12
  • 1
    @user202729 IMHO _"means that the result of (void)a is discarded."_ describes it better. The result of the C-style cast is discarded. – Thomas Sablik Feb 18 '21 at 10:14
  • 1
    @ThomasSablik - Technically, it is both. `(AnyType)a` has the effect of evaluating `a` and converting the result to `AnyType` (if the conversion is valid). Putting that in a statement on its own, has the effect of discarding the result of `(AnyType)a`. The particular case where `AnyType` is `void` has the effect of discarding `a`, and then discarding the result of discarding `a`. In any event, most modern compilers can detect that `(void)a` alone in a statement where `a` is an `int` has no net effect, and will most likely not emit any instructions at all, except in debug builds. – Peter Feb 18 '21 at 10:47
  • @Peter Yes, that's clear. But IMHO _"The result of the expression is discarded, which, in your case, means that the result of `(void)a` is discarded."_ is a better answer than _"It means that the result of `a` is discarded."_ even though both are correct. The former better describes why `a` isn't changed. `(void)a` is a cast and the result of the cast is discarded. It doesn't change `a`. I've never said that one of the sentences is wrong. – Thomas Sablik Feb 18 '21 at 10:49
  • The point of "The result of the expression is discarded, which, in your case, means that the result of (void)a is discarded." was to make things a little easier to understand. It is clear from the question that OP read the standard, so there was no point in re-iterating what the standard says, which is: "Any expression can be explicitly converted to type cv void. The expression value is discarded" (in this case `a` being the expression). – icebp Feb 18 '21 at 10:54