Error 83 error C2398: conversion from 'double' to 'float' requires a narrowing conversion

Humam Helfawi picture Humam Helfawi · Nov 9, 2015 · Viewed 13.9k times · Source

I have found many posts about this error but I could find how to overcome it. This is the code where the error is triggered:

void main(){
    float f{1.3};
}

Why in the initialize-list no casting happened like any other variable? For example, this works smoothly:

float f = 1.3;

Answer

user743382 picture user743382 · Nov 9, 2015

You've commented that the use of 1.3 gives an error with your compiler. That means you've found a compiler bug. The standard is quite clear that this is not a narrowing conversion, so it should be allowed.

Quoting N4140 (roughly C++14):

8.5.4 List-initialization [dcl.init.list]

7 A narrowing conversion is an implicit conversion

[...]

-- (7.2) 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

[...]

Your 1.3 is a constant expression well within the range of float.

I recommend reporting this to Microsoft, assuming it isn't a known issue already. Unfortunately, simply upgrading your Visual Studio won't fix this. I can reproduce the problem in VS2015.