简体   繁体   English

(void)obj和void(obj)之间的区别

[英]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. 根据http://en.cppreference.com/w/cpp/language/explicit_cast,C风格的演员和功能演员是等效的。 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. 虽然(void)arr编译,但void(arr)却没有。 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变量

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

thus you're trying to create a void variable type (with an existing name) . 因此, 您正在尝试创建一个void变量类型(具有现有名称)

And that's wrong because: 那是错的,因为:

  1. You're trying to create a void variable 您正在尝试创建一个void变量

  2. You're trying to use an existing name for another variable in the same scope 您正在尝试将现有名称用于同一范围内的另一个变量

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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