xan1242 / NFS-XtendedInput

NFS (Black Box, MW & newer) - XInput Plugin
MIT License
101 stars 7 forks source link

[Compiler] MSVC compiler version 19.33.31629 won't compile due to a template error #36

Closed xan1242 closed 2 years ago

xan1242 commented 2 years ago

This started happening after I've updated my Visual Studio to the latest available version.

Severity    Code    Description Project File    Line    Suppression State
Error   C2975   'V': invalid template argument for 'compiler_stringhash_template', expected compile-time constant expression    NFS_XtendedInput    E:\NFS-XtendedInput-master\NFS_XtendedInput_XInputConfig.h  132 

Specifically, it's complaining about this: https://github.com/xan1242/NFS-XtendedInput/blob/7bb5ec575ccdbdf410dc8dd7c9682c59eb5dc466/NFS_XtendedInput.h#L3

https://github.com/xan1242/NFS-XtendedInput/blob/7bb5ec575ccdbdf410dc8dd7c9682c59eb5dc466/NFS_XtendedInput.h#L21

It seems like a recurring issue: Link

I'll need some magicians to figure this one out...

For reference: I have Visual Studio 2022 17.3.3

I've also tested this on my laptop which has VS2022 17.0.5 and it works over there.

xan1242 commented 2 years ago

For SOME reason this works standalone:

#include <iostream>

template <int V> static constexpr int compiler_stringhash_template = V;
static constexpr int compiler_stringhash(const char* str)
{
    if (str == NULL)
        return 0;

    char* _str = ((char*)str);
    int result = -1;

    while (*_str != 0)
    {
        result = result * 0x21 + (unsigned int)(*_str);
        _str = _str + 1;
    }

    return result;
}

#define NFS_HASH(s) compiler_stringhash_template<compiler_stringhash(s)>

int main()
{
    std::cout << NFS_HASH("test");
}

I don't understand what's going on anymore...

xan1242 commented 2 years ago

Using NFS_HASH("test"); works, but using NFS_HASH("XINPUT_GAMEPAD_DPAD_UP"); doesn't...

image

The plot thickens...

This seems to have broken with MSVC 19.32

xan1242 commented 2 years ago

clang provided an ACTUAL explanation of what happened...

<source>:12:19: note: value 2241911859 is outside the range of representable values of type 'int'
                result = result * 0x21 + (unsigned int)(*_str);

THANK YOU CLANG FOR BEING USEFUL... and useless at the same time, but eh, what can we do about growing pains of compilers...

xan1242 commented 2 years ago

fixed in https://github.com/xan1242/NFS-XtendedInput/commit/5154cba46949f9ac7a13ac74403740a975988fd8