Open sylveon opened 6 years ago
What's the definition of SColour? I couldn't get the bug repro with my made up version:
struct SColour
{
int h, r, g, b;
int s;
int v;
constexpr void UpdateRGB() {}
};
The SColour.hpp header, which has all information needed, is included in the gist.
Oh, I didn't see it before. Thanks for the repro -- it repros in VS 2017 too so I'll let the team know.
Adding the typecast below is a "workaround" -- I don't know for sure if the error is valid or not according to the language spec (conversion from float to unsigned short is a data loss warning, but compiles as you've shown above):
tempcol.h = tempcol.h - (unsigned short)step;
Also, I was able to simply the repro to just:
constexpr const float cfunc()
{
unsigned short h = 0;
const float step = 359;
h = h - step;
return (float)h;
}
inline const auto &func()
{
static constexpr auto value = cfunc();
return value;
}
int main2()
{
return (int)func();
}
I tried getting a minimal repro but apparently I didn't try hard enough 😅
Figured it out: implicit narrowing in constexpr is not allowed. The IntelliSense engine is in the right, the compiler is in the wrong @sean-mcmanus.
Your cast makes the error disappear, because the narrowing is now explicit, and the constexpr becomes valid.
Our IntelliSense parser is actually supposed mimic the C/C++ language errors that exist in particular versions of compilers, so from our perspective it's still a bug. But making your code conform to the standard is probably a good idea too, in case future compilers fix the error.
I couldn't find a VS bug on this, and it still repro's in VS 2019. I opened a new bug for it: https://developercommunity.visualstudio.com/content/problem/900784/intellisense-squiggle-on-use-of-a-constexpr-functi.html
The developer community ticket was resolved as a duplicate of another issue which took a different turn. I'll re-report this. I changed the repro a little to be a little closer to the original and avoid underflow. https://godbolt.org/z/bG48YdTK4
internal ticket (2256548)
Type: LanguageService
Describe the bug
To Reproduce Steps to reproduce the behavior: (The default C++ properties were used)
expression must have a constant value -- conversion from "float" to "unsigned short" is invalid in constant-expression evaluation
Expected behavior No error, like msvc and clang do while building the snippet: