且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

表达式 "j = ++(i | i);和 j = ++(i & i);应该是左值错误?

更新时间:2023-02-12 19:15:18

你说得对,它不应该编译,而且在大多数编译器上,它不编译.
(请具体说明哪个编译器/版本没有给你一个编译器错误)

You are right that it should not compile, and on most compilers, it does not compile.
(Please specify exactly which compiler/version is NOT giving you a compiler error)

我只能假设编译器知道 (i | i) == i(i & i) == i 的身份并且正在使用它们身份来优化表达式,只留下变量 i.

I can only hypothesize that the compiler knows the identities that (i | i) == i and (i & i) == i and is using those identities to optimize away the expression, just leaving behind the variable i.

这只是一个猜测,但对我来说很有意义.

This is just a guess, but it makes a lot of sense to me.