false implicitly convert to null pointer

后端 未结 1 896
灰色年华
灰色年华 2020-12-11 16:42

Whether false is allowed to be implicitly converted to pointer is different between clang++ and g++:

g++-4.8: always a warning with or without -std=c++1

相关标签:
1条回答
  • 2020-12-11 16:55

    I'd say clang with C++11 is right:

    3.9.1 Fundamental types [basic.fundamental]

    6 Values of type bool are either true or false. [ Note: There are no signed, unsigned, short, or long bool types or values. — end note ] Values of type bool participate in integral promotions (4.5).

    bool does not have value zero, so can not be converted to null pointer:

    4.10 Pointer conversions [conv.ptr]

    1 A null pointer constant is an integral constant expression (5.19) prvalue of integer type that evaluates to zero or a prvalue of type std::nullptr_t. A null pointer constant can be converted to a pointer type;

    One might suggest conversion sequence consisting of integral promotion (bool to int) and null pointer conversion, but it would not be valid:

    4 Standard conversions [conv]

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

    [ Note: A standard conversion sequence can be empty, i.e., it can consist of no conversions. — end note ] A standard conversion sequence will be applied to an expression if necessary to convert it to a required destination type.

    0 讨论(0)
提交回复
热议问题