Fix compiler warnings for MSVC

Fixes #62. This fixes compiler warnings that are raised by MSVC.

In one case the code that was warned about was never executed, but
this compiles it out in that case to silence the warning.
This commit is contained in:
Jarryd Beck 2017-09-04 17:55:52 +10:00
parent 1e51db16b5
commit 0b7686949d

View File

@ -448,14 +448,14 @@ namespace cxxopts
{ {
if (negative) if (negative)
{ {
if (u > U(-std::numeric_limits<T>::min())) if (u > static_cast<U>(-std::numeric_limits<T>::min()))
{ {
throw argument_incorrect_type(text); throw argument_incorrect_type(text);
} }
} }
else else
{ {
if (u > std::numeric_limits<T>::max()) if (u > static_cast<U>(std::numeric_limits<T>::max()))
{ {
throw argument_incorrect_type(text); throw argument_incorrect_type(text);
} }
@ -479,6 +479,23 @@ namespace cxxopts
} }
} }
template <typename R, typename T>
R
checked_negate(T&& t, const std::string&, std::true_type)
{
// if we got to here, then `t` is a positive number that fits into
// `R`. So to avoid MSVC C4146, we first cast it to `R`.
// See https://github.com/jarro2783/cxxopts/issues/62 for more details.
return -static_cast<R>(t);
}
template <typename R, typename T>
T
checked_negate(T&&, const std::string& text, std::false_type)
{
throw argument_incorrect_type(text);
}
template <typename T> template <typename T>
void void
integer_parser(const std::string& text, T& value) integer_parser(const std::string& text, T& value)
@ -537,11 +554,14 @@ namespace cxxopts
if (negative) if (negative)
{ {
if (!is_signed) value = checked_negate<T>(result,
{ text,
throw argument_incorrect_type(text); std::integral_constant<bool, is_signed>());
} //if (!is_signed)
value = -result; //{
// throw argument_incorrect_type(text);
//}
//value = -result;
} }
else else
{ {