Narrowing for literals that cannot be exactly represented
9.4.5 [dcl.init.list]
Jason Merrill

Created on 2008-07-02.00:00:00 last changed 143 months ago


Date: 2009-10-15.00:00:00

[Voted into WP at October, 2009 meeting.]

Date: 2009-07-15.00:00:00

Proposed resolution (July, 2009):

Change 9.4.5 [dcl.init.list] paragraph 6 as follows:

A narrowing conversion is an implicit conversion

  • from a floating-point type to an integer type, or

  • from long double to double or float, or from double to float, except where the source is a constant expression and the actual value after conversion will fit into the target type and will produce the original value when converted back to the original type is within the range of values that can be represented (even if it cannot be represented exactly), or

  • ...

Date: 2008-09-15.00:00:00

Notes from the September, 2008 meeting:

The CWG agreed that the second initialization should not be a narrowing error; furthermore, this exemption should apply not only to literals but to any floating-point constant expression. Instead of the current formulation, requiring exact bidirectional convertibility, the Standard should only require that the initializer value be within the representable range of the target type.

Date: 2008-07-02.00:00:00

Both of the following initializations are ill-formed because of narrowing, although they were previously well-formed:

    struct A { int i; } a = { 1.0 };
    struct B { float f; } b = { 1.1 };

The first one doesn't seem like a big problem, as there probably isn't much code that has this kind of aggregate initialization. The second might be of more concern, because 1.1 is not representable in either float or double. Is the resulting loss of precision a kind of narrowing that we want to diagnose?

Date User Action Args
2010-03-29 00:00:00adminsetstatus: dr -> cd2
2009-11-08 00:00:00adminsetmessages: + msg2462
2009-11-08 00:00:00adminsetstatus: ready -> dr
2009-08-03 00:00:00adminsetmessages: + msg2166
2009-08-03 00:00:00adminsetstatus: drafting -> ready
2008-10-05 00:00:00adminsetmessages: + msg1781
2008-10-05 00:00:00adminsetstatus: open -> drafting
2008-07-02 00:00:00admincreate