Can (a==1)&&(a==2)&&(a==3) evaluate to true? (and can it be useful?)

前端 未结 8 1337
北荒
北荒 2021-01-15 11:34

Inspired by another question regarding java-script language. Can the expression

 (a==1)&&(a==2)&&(a==3)

evaluate to true i

8条回答
  •  北海茫月
    2021-01-15 12:10

    Other things not mentioned yet (source):

    • a might have overloaded operator int(), the operator for implicit conversion to int (instead of operator== as covered by other answers).
    • a might be a preprocessor macro.

    Example of the latter:

    int b = 0;
    #define a ++b
    if ((a==1)&&(a==2)&&(a==3))
        std::cout << "aha\n";
    

提交回复
热议问题