Why can't nullptr convert to int?

后端 未结 4 2133
不知归路
不知归路 2021-02-07 09:32

Summary: nullptr converts to bool, and bool converts to int, so why doesn\'t nullptr convert to int

4条回答
  •  猫巷女王i
    2021-02-07 10:01

    In §4.1 of the Standard, it says how conversions are performed:

    Standard conversions are implicit conversions with built-in meaning. Clause 4 enumerates the full set of such conversions. A standard conversion sequence is a sequence of standard conversions in the following order:

    — Zero or one conversion from the following set: lvalue-to-rvalue conversion, array-to-pointer conversion, and function-to-pointer conversion.

    — Zero or one conversion from the following set: integral promotions, floating point promotion, integral conversions, floating point conversions, floating-integral conversions, pointer conversions, pointer to member conversions, and boolean conversions.

    — Zero or one qualification conversion.

    So the compiler only does "zero or one conversion" of some, none, or all of each of the above types of conversions, not arbitrarily many. And that's a really good thing.

提交回复
热议问题