float initialization from double with braces
Asked Answered
I

2

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

Impart answered 25/11, 2016 at 13:10 Comment(2)
afaik, it does not warn you if a particular literal value can be represented in a narrower type without precision lossForbiddance
This doesn't compile on VS2015 (error 2397)Gaullist
S
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.

Swayback answered 25/11, 2016 at 13:16 Comment(0)
G
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

Griefstricken answered 25/11, 2016 at 13:15 Comment(7)
When you say should be an error, not a warning, I would clarify that the standard does not require that be an error.Swayback
@user2079303 The standard says "such conversions are not allowed", so I think warning is not the case, isn't it?Griefstricken
your are right but it does not give a warning also for longer inputsImpart
@Griefstricken 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.Swayback
@Impart See my edited answer, (if I got your point correctly).Griefstricken
@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.Griefstricken
@Griefstricken 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.Swayback

© 2022 - 2024 — McMap. All rights reserved.