2

I am studying C++ from C++ Primer by Josée Lajoie and Stanley B. Lippman. I read that when variables are initialized using curly braces and if the initialization results in loss of data then an error occurs.

double a=5.545;
int b{a};
std::cout<<b;

But when I run the above code I am not getting any error and getting the output as 5.

PS: I am using CLion IDE and MingW C++ compiler.

logdev
  • 292
  • 6
  • 22

1 Answers1

1

The book is wrong. Construction of an int from a double is perfectly normal and commonplace, and it will lose data (all the fractional parts!). This is not in any way an error.

Your compiler may warn you about the loss of data if it can be sure that there will be some. That's it being kind. But the general principle is that, by writing this code, you literally said "I want to lose the fractional parts please".

If you turn on "warnings as errors", then any such warning gets converted into an error and halts the build. But that's not related to the conversion itself.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • 3
    The book is definitely correct. [List initialization doesn't allow such narrowing](http://en.cppreference.com/w/cpp/language/list_initialization#Narrowing_conversions). – llllllllll Feb 16 '18 at 11:47
  • List-initialization does not apply here, because int is a fundamental type and does not have a [constructor](https://stackoverflow.com/questions/5113365/do-built-in-types-have-default-constructors). – rbf Feb 16 '18 at 12:03
  • @FlorianM. Please read the link above, the first clause of prohibition is "conversion from a floating-point type to an integer type". This is [how clang correctly rejects OP's code](https://godbolt.org/g/NJXR92) without any special warning level. – llllllllll Feb 16 '18 at 12:10
  • @liliscent: Hmm seems you're right - however, the end result is still compliant. https://stackoverflow.com/a/12874041/560648 – Lightness Races in Orbit Feb 16 '18 at 12:26
  • @liliscent I have read the link carefully before I posted my answer. And I think that list-initialization is not used here, because that would require a constructor (IMHO). But there is another point, which I overlooked: Otherwise (if T is not a class type), if the braced-init-list has only one element and either T isn't a reference type or is a reference type that is compatible with the type of the element, T is direct-initialized (in direct-list-initialization) or copy-initialized (in copy-list-initialization), except that narrowing conversions are not allowed. --> So I agree with you – rbf Feb 16 '18 at 14:15
  • It's not a good answer if it declares code that is not valid C++ to not contain errors "in any way". You are right that a compiler is not required to emit an error, and in such a way to can even say that "int my main to me;" is not in any way an error, because your compiler may just emit a warning about it (and then does whatever). But this isn't useful at all. – Johannes Schaub - litb Feb 17 '18 at 15:40
  • @JohannesSchaub-litb: We've already established that – Lightness Races in Orbit Feb 17 '18 at 15:41
  • So while you are right about the compiler and the distinction between error messages and diagnostics, you are wrong about your initial statement "The book is wrong". It is actually right: An error occurs in compilers in which the diagnostic message is given as an error message for that invalid C++ snippet. – Johannes Schaub - litb Feb 17 '18 at 15:43
  • @JohannesSchaub-litb: Yes, I agree - again, we have already discussed all of this. – Lightness Races in Orbit Feb 17 '18 at 15:44
  • Have you ever tried deleting an accepted answer? – Lightness Races in Orbit Feb 17 '18 at 15:48
  • @LightnessRacesinOrbit I'm with you on this. I know this feeling. https://meta.stackoverflow.com/questions/340966/what-should-i-do-when-my-accepted-answer-turns-out-to-be-inappropriate – Johannes Schaub - litb Feb 18 '18 at 17:55
  • @JohannesSchaub-litb: I remember that one :) – Lightness Races in Orbit Feb 18 '18 at 18:02