在未初始化的变量上使用复合赋值运算符(+=,...)不是 C++ 中的 UB?

Is using compound assignment operator (+=, ...) on uninitialized variable NOT a UB in C++?

我正在尝试创建一个简单的工具来检测基于 Clang AST 的未初始化变量的使用。我所知道的是,实际上导致带有 uninit 变量的 UB 是隐式发生的左值到右值的转换。

现在,我在检查一个基本示例程序的 AST 时注意到,所有复合赋值运算符 不会 导致出现任何此类强制转换节点!这是否意味着 没有 UB 发生?

int a;
int b = 10;
b += a; // this line is obviously UB...
a += b; // ... but is this one ok?

// Note: no ImplicitCastExpr of LvalueToRvalue type in AST !

后缀/前缀递增/递减运算符也是如此(特别是,我完全不知道后缀运算符如何在不复制的情况下保存变量的“”) .

我设法找到了一些关于增量运算符的信息( - 不幸的是,只有一个参考),但现在却在 comp 上苦苦挣扎。作业。如果可能的话,我也很想知道特别是增量会发生什么。

Is using compound assignment operator (+=, …) on uninitialized variable NOT a UB in C++?

不,它 UB(标准规定不是的情况除外)。

标准引语(来自最新草稿):

[expr.ass] The behavior of an expression of the form E1 op= E2 is equivalent to E1 = E1 op E2 except that E1 is evaluated only once.

所以,我们知道左手操作数被求值了。而我们也知道在运算中使用了这个值

[basic.indet] If an indeterminate value is produced by an evaluation, the behavior is undefined ...

给你。