gpt4 book ai didi

c++ - "void();"作为单独的语句在 C++ 中意味着什么?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:55:04 24 4
gpt4 key购买 nike

这个程序是如何编译好的?

int main() {
void(); // Does this create a "void" object here?
}

我已经在 MSVC 和 GCC 下进行了测试。但是 void 是一个不完整的类型。当您对任何其他不完整的用户定义类型执行相同操作时,

class Incomplete;

int main() {
Incomplete(); // Error saying "Incomplete" is incomplete.
}

最佳答案

C++11 §5.2.3 [expr.type.conv]/2 详细介绍(强调我的):

The expression T(), where T is a simple-type-specifier or typename-specifier for a non-array complete object type or the (possibly cv-qualified) void type, creates a prvalue of the specified type, whose value is that produced by value-initializing (8.5) an object of type T; no initialization is done for the void() case.

它只是 void 类型的纯右值。没有特殊的初始化或类似 int() 的东西。纯右值类似于 truenullptr2。该表达式等效于 2;,但用于 void 而不是 int

关于c++ - "void();"作为单独的语句在 C++ 中意味着什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25835324/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com