gpt4 book ai didi

c++ - 这是未定义的行为还是 struct init 的错误?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:26:41 26 4
gpt4 key购买 nike

请考虑这段代码:

#include <iostream>

int main()
{
struct A
{
int x;
int y;
int z;

int foo()
{
std::cout << "enter foo: " << this->x << "," << this->y << "," << this->z << std::endl;
return 5;
}

int moo()
{
std::cout << "enter moo: " << this->x << "," << this->y << "," << this->z << std::endl;
this->x = 1;
this->z = 10;
return 2;
}
};

A b { b.foo(), b.z = b.moo(), 3};

std::cout << "final: " << b.x << "," << b.y << "," << b.z << std::endl;

return 0;
}

我的 VS2017(x64 版本)中的结果:

enter foo: 0,0,0
enter moo: 5,0,0
final: 1,2,3

ideone.com (gcc 6.3) 的结果 https://ideone.com/OGqvjW ):

enter foo: 0,0,3
enter moo: 5,0,3
final: 1,2,2

一个编译器立即将 z 成员设置为 3,在一切之前,然后在调用方法和赋值时覆盖它,另一个在最后,在一切之后进行。

问。对这种行为的解释是什么?

谢谢。

最佳答案

是的,这是未定义的行为:

int foo()
{
std::cout << "enter foo: " << this->x << "," << this->y << "," << this->z << std::endl;
// ~~~~~~~ ~~~~~~~ ~~~~~~~
}

在调用foo() 时,xyz 尚未被调用尚未初始化。来自 [dcl.init]/12 :

If no initializer is specified for an object, the object is default-initialized. When storage for an object with automatic or dynamic storage duration is obtained, the object has an indeterminate value, and if no initialization is performed for the object, that object retains an indeterminate value until that value is replaced ([expr.ass]). [...] If an indeterminate value is produced by an evaluation, the behavior is undefined except in the following cases: [...]

其余情况均不适用。因此打印 xyz 存在未定义的行为。与只是:

int x;
std::cout << x; // ub

我之前的回答是肯定的,但出于终生原因。它建议 A b{ b.foo(), b.z = b.moo(), 3}; 中的初始化是非空的,因此 的任何成员的任何访问>b 初始化结束前为UB。然而,xskxzr 已经向我指出,为了使初始化成为非空的,你 must have constructors invoked , 而 int 没有构造函数。这使得 b 的初始化变得空洞。这在我看来在概念上很奇怪,但是这方面的措辞很清楚。

关于c++ - 这是未定义的行为还是 struct init 的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48035195/

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