When does an rvalue get invalidated/is considered undefined?
(右值什么时候失效/被认为是未定义的?)
Below are two examples, one of which the rvalue is stored in a local variable and then actions on that local variable are performed, the other example shows a member of the rvalue being passed immediately to a function which also operates on the data.
(下面是两个示例,其中一个rvalue存储在一个局部变量中,然后对该局部变量执行操作,另一个示例显示该rvalue的成员立即传递给对数据进行运算的函数。)
I suspect example 1 is undefined behaviour, as in my own code (this is a minimal reproducible example), the application completely fails, meanwhile 2 does not.
(我怀疑示例1是未定义的行为,因为在我自己的代码中(这是最小的可复制示例),应用程序完全失败,而2则没有。)
Is 2 also undefined behaviour? (2也是不确定的行为吗?)
Which of the examples are? (哪个例子是?)
struct container {
int data[5];
container(int a) {
data[0] = a;
}
};
void main() {
int* arr = container(123).data;
// ... do stuff with data
}
vs
(与)
struct container {
int data[5];
container(int a) {
data[0] = a;
}
};
void do_stuff_with_data(int* data) {
// ... do stuff with data
}
void main() {
do_stuff_with_data(container(123).data);
}
ask by super translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…