double d = 3.9; int x{d}; What does a conforming C++11 compiler do with this declaration?
Correct answer
D. Rejects it, because narrowing conversions are ill-formed in braced init
19 views
D. Rejects it, because narrowing conversions are ill-formed in braced init
19 views
Braced initialisation forbids narrowing conversions, so converting a double to int inside int x{d} is ill-formed; the copy form int x = d; is merely a warning-level narrowing. The rule exists to stop silent loss of precision and also rejects long to int and int to char when the value cannot be represented. Compilers must diagnose it, though the diagnostic is often an error rather than a warning.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is D? Ask here and get an explanation.