4

According to http://en.cppreference.com/w/cpp/language/explicit_cast, C-style cast and functional cast are equivalent. However, see the following example:

#include <array>
int main() {
  std::array<int, 3> arr{};
  (void)arr;
  //void(arr);
}

While (void)arr compiles, void(arr) does not. What have I missed?

Lingxi
  • 14,579
  • 2
  • 37
  • 93
  • What error message do you get? – TobiMcNamobi Jul 18 '14 at 10:43
  • `void(arr)` will be parsed as a function call surely. What do you think casting to `void` even means? Because it means nothing! – David Heffernan Jul 18 '14 at 10:44
  • 5
    Casting to `void` is valid, and it means the result of the expression is discarded. It's often used to silence unused variable warnings, and it has a use in TMP to prevent overload resolution selecting overloaded comma operators. – Simple Jul 18 '14 at 10:49
  • Valid alternative syntaxes for an expression statement casting to void include: `( void(arr) );` and `static_cast(arr);`. – CB Bailey Jul 18 '14 at 11:00
  • 1
    @DavidHeffernan: No, it will be parsed as a variable declaration, giving errors because a variable can't have type `void`, and there's already a variable called `arr`. – Mike Seymour Jul 18 '14 at 11:55
  • @MikeSeymour How does the compiler tell the difference between that and a function call? – David Heffernan Jul 18 '14 at 11:56
  • @DavidHeffernan: `void` can't be the name of a function, so `void(arr)` can't be a function call. (Even if it could, the Most Vexing Parse would resolve the ambiguity by interpreting it as a declaration if possible). – Mike Seymour Jul 18 '14 at 11:58
  • @MikeSeymour So, if I had a type named `x`, and a function named `x`, then `x(y);` would be a declaration? – David Heffernan Jul 18 '14 at 12:06
  • @DavidHeffernan: No, it seems that that particular ambiguity gives an error rather than arbitrarily being interpreted as a declaration. Sorry for misleading you. – Mike Seymour Jul 18 '14 at 12:12
  • @DavidHeffernan: Name lookup will determine whether `x` in that context refers to the type or the function and the rest of the expression will be evaluated accordingly. – CB Bailey Jul 18 '14 at 12:26

2 Answers2

5

If there are no ambiguities (e.g. other functions with the same name, macros..) involved, the following code declares and defines two int variables

int a = 22;
int (b) = 33;

thus you're trying to create a void variable type (with an existing name).

And that's wrong because:

  1. You're trying to create a void variable

  2. You're trying to use an existing name for another variable in the same scope

Community
  • 1
  • 1
Marco A.
  • 43,032
  • 26
  • 132
  • 246
  • I guess I will stick to the syntax `(type)(var)` from now on to avoid this ambiguity. C++ style casts are unambiguous, but on the other hand way too nasty. – Lingxi Jul 18 '14 at 12:56
4

While void(arr) is usually the same as (void)arr, in this context it is a definition, and you are trying to create a variable called arr of type void, which isn't allowed.

Simple
  • 13,992
  • 2
  • 47
  • 47