gpt4 book ai didi

c++ - 为什么可以跳入不带初始值设定项的标量类型对象的范围?

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

当我阅读 C++ 标准时,根据标准,以下代码似乎完全没问题。

int main() {
goto lol;
{
int x;
lol:
cout << x << endl;
}
}

// OK

[n3290: 6.7/3]: It is possible to transfer into a block, but not in a way that bypasses declarations with initialization. A program that jumps from a point where a variable with automatic storage duration is not in scope to a point where it is in scope is ill-formed unless the variable has scalar type, class type with a trivial default constructor and a trivial destructor, a cv-qualified version of one of these types, or an array of one of the preceding types and is declared without an initializer.

为什么它会起作用?跳过它的定义并使用未定义的 x 不是仍然很危险吗?为什么初始化程序的存在会有所不同?

最佳答案

无论如何,您都会使用未初始化的 x,因为 int x; 是未初始化的。初始化程序的存在当然会有所不同,因为您会跳过它。 int x = 5; 例如初始化 x,所以如果你跳过它会有所不同。

关于c++ - 为什么可以跳入不带初始值设定项的标量类型对象的范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8529383/

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