0

I have ran into this issue on a set of code that worked fine before.

ImGui::SetNextWindowSize({ 140.f, screen_height - 68 });

  • 1
    if `screen_height` is float, change element 2 to `screen_height - 68.f`. – Zem Dec 26 '19 at 08:44
  • It seems that `ImGUI::SetNextWindowSize()` requires a pair of `float`s. The second parameter probably results to type `int`. (Hard to say without knowing how `screen_height` is defined.) While `int` has usually 32 bit nowadays, a `float` can map only up to 2^23 without loss of precision. So, the warning is justified but probably not that critical in your case. If you want to get it away you may cast `screen_height` to `float` explicitly: `(float)screen_height - 68.f` or `(float)(screen_height - 68)`. – Scheff's Cat Dec 26 '19 at 08:46
  • 1
    Does this answer your question? [narrowing conversion from unsigned to double](https://stackoverflow.com/questions/11521016/narrowing-conversion-from-unsigned-to-double) – Scheff's Cat Dec 26 '19 at 08:50
  • @Scheff It should not be a warning. The narrowing conversion should make the program ill-formed. It is just that GCC is too lenient. If you add the `-pedantic-errors` flag, it will give a proper error. Clang and MSVC give proper errors without any flags. – walnut Dec 26 '19 at 08:55
  • @walnut I wouldn't be that stricht. 2^23 is a damn big number concerning screen height. This is something the compiler cannot know but the author is able to justice. Therefore, I find a warning appropriate. And, in case, `-pedantic-errors` is available to turn warnings into errors. – Scheff's Cat Dec 26 '19 at 08:58
  • @Scheff It is at least not portable, since only GCC considers it warning-only. It can easily be made portable by making `68` a floating point number as you suggested or an explicit `static_cast`. The latter would also make it clear that you are aware of the narrowing and that it should not be a problem, while the implicit cast doesn't. – walnut Dec 26 '19 at 09:04
  • "that worked fine before" If you do not remember what you changed betwee functional and broken code, then it is time to start using a versioning system. – Yunnosch Dec 26 '19 at 09:08
  • 1
    @walnut Compilers are not mandated to give error and stop compiling. [Does the C++ standard specify that for some cases the compiling should fail with an error?](https://stackoverflow.com/q/40807404/3309790) – songyuanyao Dec 26 '19 at 09:11
  • @songyuanyao Yes, I didn't mean to say that GCC *has to* give an error. It is only my personal opinion that it is not helpful to give only a warning, especially if other compilers give error messages. – walnut Dec 26 '19 at 09:31

0 Answers0