Title
Converting from nullptr_t to bool in overload resolution
Status
cd5
Section
12.2.2.6 [over.match.conv]
Submitter
Hubert Tong

Created on 2013-09-26.00:00:00 last changed 7 months ago

Messages

Date: 2018-06-15.00:00:00

Proposed resolution (June, 2018):

  1. Change 7.3.15 [conv.bool] paragraph 1 as follows:

  2. A prvalue of arithmetic, unscoped enumeration, pointer, or pointer-to-member type can be converted to a prvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true. For direct-initialization (9.4 [dcl.init]), a prvalue of type std::nullptr_t can be converted to a prvalue of type bool; the resulting value is false.
  3. Add the following bullet before 9.4 [dcl.init] bullet 17.8:

  4. The semantics of initializers are as follows. The destination type is the type of the object or reference being initialized and the source type is the type of the initializer expression. If the initializer is not a single (possibly parenthesized) expression, the source type is not defined.

    • ...

    • Otherwise, if the initialization is direct-initialization, the source type is std::nullptr_t, and the destination type is bool, the initial value of the object being initialized is false.

    • Otherwise, the initial value of the object being initialized is the (possibly converted) value...

  5. Change 12.2.4.3 [over.ics.rank] bullet 4.1 as follows:

  6. Standard conversion sequences are ordered by their ranks: an Exact Match is a better conversion than a Promotion, which is a better conversion than a Conversion. Two conversion sequences with the same rank are indistinguishable unless one of the following rules applies:

    • A conversion that does not convert a pointer, or a pointer to member, or std::nullptr_t to bool is better than one that does.

    • ...

This resolution also resolves issue 2133.

Date: 2018-11-15.00:00:00

[Accepted as a DR at the November, 2018 (San Diego) meeting.]

According to 12.2.2.6 [over.match.conv] paragraph 1, when a class type S is used as an initializer for an object of type T,

The conversion functions of S and its base classes are considered. Those non-explicit conversion functions that are not hidden within S and yield type T or a type that can be converted to type T via a standard conversion sequence (12.2.4.2.2 [over.ics.scs]) are candidate functions.

Because conversion from std::nullptr_t to bool is only permitted in direct-initialization (7.3.14 [conv.fctptr]), it is not clear whether there is a standard conversion sequence from std::nullptr_t to bool, considering that an implicit conversion sequence is intended to model copy-initialization. Should 12.2.2.6 [over.match.conv] be understood to refer only to conversions permitted in copy-initialization, or should the form of the initialization be considered? For example,

  struct SomeType {
    operator std::nullptr_t();
  };
  bool b{ SomeType() };    // Well-formed?

Note also 12.2.4.3 [over.ics.rank] paragraph 4, which may bear on the intent (or, alternatively, might describe a situation that cannot arise):

A conversion that does not convert a pointer, a pointer to member, or std::nullptr_t to bool is better than one that does.

See also issues 2133 and 2243.)

History
Date User Action Args
2020-12-15 00:00:00adminsetmessages: + msg6409
2020-12-15 00:00:00adminsetstatus: open -> cd5
2013-09-26 00:00:00admincreate