0

So I have seen this quest a few times and I have tried wrapping it with static_cast(WIDTH) and static_cast(HEIGHT) however it just produces the same error and I am not too sure why it seem to me int to float shouldnt even need a narrowing conversion. I am currently learning ImGui and am getting this error when i compile Error C2398 Element '1': conversion from 'const int' to 'float' requires a narrowing conversion

This is my code

constexpr int WIDTH = 500; 
constexpr int HEIGHT = 300;

}
//it is referenced later in this line in a different file that DOES include the header

void gui::Render() noexcept
{
ImGui::SetNextWindowSize({ WIDTH, HEIGHT });
}

//I have attempted to do a simple float conversion but it still screams at me with the same error
Paul Sanders
  • 24,133
  • 4
  • 26
  • 48
  • 1
    What is the type of parameters of the function SetNextWindowSize? – Vlad from Moscow May 04 '22 at 21:07
  • Seems to be `ImVec2`, which is `struct {float x,y;}`, but I may be reading the wrong documentation – Lala5th May 04 '22 at 21:10
  • 1
    Huh this is weird. The [standard](https://eel.is/c++draft/dcl.init.list#7.3) seems to suggest that there shouldn't be an issue, since these are constant expressions that can be represented in float. Maybe bug? Either way you can get out of this by not using init lists as it seems – Lala5th May 04 '22 at 21:14
  • What compiler version are you using. Tried emulating your error, but none of the [3 major compilers](https://godbolt.org/z/8985h8bM8) seem to throw this error, however older versions of MSVC do complain about possible loss of precision – Lala5th May 04 '22 at 21:23
  • I am using Microsoft Visual Studio (not VS code) version 15.9.47 Windows SDK 10.0.17763.0 // SubSytem:Windows Micro Soft Native Recommended Rules – Exiled Pho May 04 '22 at 21:50
  • You have got your braces in a tangle (but I fixed them for you , I hope correctly). – Paul Sanders May 04 '22 at 22:53
  • @Lala5th _Huh this is weird_ [indeed](https://wandbox.org/permlink/lW07AUREigRcIK9s). [ edit ] Seems to be an MSVC thing, see: https://godbolt.org/z/z4e5cv1sd. I can't go back as far as you OP, but if you try that example with the latest version of the compiler (which you can do for yourself at Godbolt) then the warning / error goes away. – Paul Sanders May 04 '22 at 23:06
  • Issue resolved it was the compiler it compiled completely fine with g++ thanks all – Exiled Pho May 06 '22 at 17:40

0 Answers0