Expressions "j = ++(i | i); and j = ++(i & i); should be a lvalue error?

前端 未结 5 1642
心在旅途
心在旅途 2020-12-02 22:03

I was expecting that in my following code:

#include 
int main(){
    int i = 10; 
    int j = 10;

    j = ++(i | i);
    printf(\"%d %d\\n\         


        
5条回答
  •  独厮守ぢ
    2020-12-02 23:05

    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 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.

提交回复
热议问题