简体   繁体   中英

Difference between (void)obj and void(obj)

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?

If there are no ambiguities (eg 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

虽然void(arr)通常与(void)arr相同,但在此上下文中它是一个定义,并且您尝试创建一个名为arr的变量,该变量类型为void ,这是不允许的。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM