Defining double exclamation?

前端 未结 7 1941
失恋的感觉
失恋的感觉 2021-02-04 06:43

I understand what a double exclamation mark does (or I think I understand) but I am not sure how it is defined on a random object. For example in the code snippet below:

7条回答
  •  孤街浪徒
    2021-02-04 07:06

    Don't think of it as "double exclamation mark", think of it as two separate operators, one running on the result of the other.

    For all primitive types, it will "work". !a is equivalent to a == 0, so !!a is equivalent to !(a == 0), which in turn is equivalent to a != 0.

    For user-defined types, it won't compile unless they overload operator !. But obviously, in this case, the behaviour could be almost anything.

提交回复
热议问题