1

The following compiles fine using GDB online, but fails when compiled with MSVC with the error:

example.cpp(64,73): warning C4146: unary minus operator applied to unsigned type, result still unsigned example.cpp(64,62): error C2398: Element '2': conversion from 'unsigned long' to 'const int' requires a narrowing conversion

#include <type_traits>
#include <cstdint>
#include <cstddef>

template < size_t N, bool SIGNED >
class Range_Consts
{
public:
    using ValueType =
        typename std::conditional<
            SIGNED,
            typename std::conditional<
                N <= 8, std::int8_t,
                typename std::conditional<
                    N <= 16, std::int16_t,
                    std::int32_t
                    >::type
                >::type,
            typename std::conditional<
                N <= 8, std::uint8_t,
                typename std::conditional<
                    N <= 16, std::uint16_t,
                    std::uint32_t
                    >::type
                >::type
            >::type;

    constexpr Range_Consts(
        const ValueType value,
        const ValueType minimum,
        const ValueType maximum
    ) :
    m_value(value),
    m_minimum(minimum),
    m_maximum(maximum)
    { }

    const ValueType m_value;
    const ValueType m_minimum;
    const ValueType m_maximum;
};

int main()
{
    static constexpr Range_Consts<32, true> MY_RANGE = { 0, -2147483648, 2147483647 };
    return 0;
}

When I hover over the squiggly in VS Code, the -2147483648 shows as (unsigned long)2147483648UL. Am I overlooking something with how I've written the ValueType conditional? How can I get MSVC to recognize the correct signage and use the respective expected type?

E-rich
  • 9,243
  • 11
  • 48
  • 79
  • See [this](https://stackoverflow.com/questions/45469214/why-does-the-most-negative-int-value-cause-an-error-about-ambiguous-function-ove/45469321#45469321) regarding what `-2147483648` actually is. – NathanOliver Oct 01 '20 at 19:47
  • [Why does the smallest int, −2147483648, have type 'long'?](https://stackoverflow.com/q/34724320/995714), [(-2147483648> 0) returns true in C++?](https://stackoverflow.com/q/14695118/995714), [Why does MSVC pick a long long as the type for -2147483648?](https://stackoverflow.com/q/34725215/995714), [C: Casting minimum 32-bit integer (-2147483648) to float gives positive number (2147483648.0)](https://stackoverflow.com/q/11536389/995714) – phuclv Oct 02 '20 at 01:47

0 Answers0