Narrowing conversions and overload resolution
Section [over.ics.list]
David Krauss

Created on 2015-08-26.00:00:00 last changed 15 months ago


Date: 2023-03-15.00:00:00

Additional notes (March, 2023)

A narrowing conversion is but one of the many circumstances that could make a conversion ill-formed despite the existence of an implicit conversion sequence. Other examples (with implementation divergence) are invoking a private or deleted function, binding a non-const reference to a bit-field, invoking an explicit constructor in list-initialization, invoking a consteval constructor with unsuitable arguments, or initializing an aggregate with incorrectly-ordered designated initializers. A resolution of this issue should thus not be focused on narrow conversions.

Date: 2016-03-15.00:00:00

Rationale (March, 2016):

This is a question of language design and thus more suited to consideration by EWG.

EWG (January, 2021):

Adjust the standard to follow existing implementations. See vote.

Date: 2022-11-20.07:54:16

Current implementations ignore narrowing conversions during overload resolution, emitting a diagnostic if calling the selected function would involve narrowing. For example:

  struct s { long m };
  struct ss { short m; };

  void f( ss );
  void f( s );
  void g() {
    f({ 1000000 }); // Ambiguous in spite of narrowing for f(ss)

However, the current wording of [over.ics.list] paragraph 7 says,

Otherwise, if the parameter has an aggregate type which can be initialized from the initializer list according to the rules for aggregate initialization (9.4.2 [dcl.init.aggr]), the implicit conversion sequence is a user-defined conversion sequence with the second standard conversion sequence an identity conversion.

In the example above, ss cannot be initialized from { 1000000 } because of the narrowing conversion, so presumably f(ss) should not be considered. If this is not the intended outcome, paragraph 7 should be restated in terms of having an implicit conversion sequence, as in, e.g., bullet 9.1, instead of a valid initialization.

Date User Action Args
2023-03-11 18:43:23adminsetmessages: + msg7227
2022-04-27 22:23:07adminsetstatus: extension -> open
2017-02-06 00:00:00adminsetmessages: + msg5998
2017-02-06 00:00:00adminsetstatus: open -> extension
2016-02-15 00:00:00adminsetstatus: drafting -> open
2015-08-26 00:00:00admincreate