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?