8

Why does the compiler (clang,gcc) not warn about narrowing conversions when doing this

float a{3.1231231241234123512354123512341235123541235};
float a = {double(3.1231231241234123512354123512341235123541235)}

I expected a warning because I do explicit value-initialization with braces. Following this answer Link it should spit out an error.

Compilation here

Community
  • 1
  • 1
Gabriel
  • 8,990
  • 6
  • 57
  • 101

2 Answers2

15

[dcl.init.list]/§7 (standard draft)

A narrowing conversion is an implicit conversion

...

  • 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 is within the range of values that can be represented (even if it cannot be represented exactly), or

...

Both expressions 3.14159 and double(3.141) are constant expressions and the value is within the range of values representable by float. Therefore the conversion isn't narrowing as defined by the standard and there is no requirement to warn about the conversion.


but it does not give a warning also for longer inputs

Sure it does, as long as the value is outside of the range of values representable by float.

Community
  • 1
  • 1
eerorika
  • 232,697
  • 12
  • 197
  • 326
10

Because the source is a constant expression and overflow does not occur for these cases, then narrowing conversion error won't be triggered.

(emphasis mine)

conversion from a long double to double or to float and conversion from double to float, except where the source is a constant expression and overflow does not occur

If you use it with a double variable(i.e. a non-constant-expression) or a constant with big value which causes overlow, the diagnostic message will be generated. e.g.

double d = 3.14159;
float a {d}; // non-constant-expression cannot be narrowed from type 'double' to 'float' in initializer list

EDIT (for longer input)

Because even if the value cannot be represented exactly by float, overflow still doesn't occur, then it's allowed.

$8.6.4/7.2 List-initialization (emphasie mine)

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 is within the range of values that can be represented (even if it cannot be represented exactly), or

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
  • 1
    When you say **should be an error, not a warning**, I would clarify that the standard does not require that be an error. – eerorika Nov 25 '16 at 13:41
  • @user2079303 The standard says "such conversions are not allowed", so I think warning is not the case, isn't it? – songyuanyao Nov 25 '16 at 13:48
  • your are right but it does not give a warning also for longer inputs – Gabriel Nov 25 '16 at 13:49
  • 1
    @songyuanyao the standard says "If a narrowing conversion (see below) is required to convert any of the arguments, the program is **ill-formed**". Elsewhere in the standard it says that compiler is required to show a diagnostic message if the program is ill-formed. No other requirements such as the message being an error and preventing compilation. – eerorika Nov 25 '16 at 13:51
  • 1
    @Gabriel See my edited answer, (if I got your point correctly). – songyuanyao Nov 25 '16 at 13:56
  • @user2079303 But #7 says "not allowed" explicitly, do you mean such description in the stardard still doesn't mean it should be error, I mean the compilation should fail for this case. – songyuanyao Nov 25 '16 at 14:01
  • 1
    @songyuanyao The note in #7 begins with "As indicated above" which refers to the rule that I quoted. I would consider it merely a reminder of the rule, rather than an overriding rule. That said, I don't think it makes any difference. I don't think the standard at any point requires that compilation must fail. When a program violates the standard, it is ill-formed (unless specified otherwise such as when there is UB instead) and there must be a message. Whether the message is an error is never specified by the standard and it is at the discretion of the compiler to allow the compilation or not. – eerorika Nov 25 '16 at 14:20