From 0b7686949d01f6475cc13ba0693725aefb76fc0c Mon Sep 17 00:00:00 2001 From: Jarryd Beck Date: Mon, 4 Sep 2017 17:55:52 +1000 Subject: [PATCH] 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. --- include/cxxopts.hpp | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/include/cxxopts.hpp b/include/cxxopts.hpp index ff17ec4..fe7d8ef 100644 --- a/include/cxxopts.hpp +++ b/include/cxxopts.hpp @@ -448,14 +448,14 @@ namespace cxxopts { if (negative) { - if (u > U(-std::numeric_limits::min())) + if (u > static_cast(-std::numeric_limits::min())) { throw argument_incorrect_type(text); } } else { - if (u > std::numeric_limits::max()) + if (u > static_cast(std::numeric_limits::max())) { throw argument_incorrect_type(text); } @@ -479,6 +479,23 @@ namespace cxxopts } } + template + 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(t); + } + + template + T + checked_negate(T&&, const std::string& text, std::false_type) + { + throw argument_incorrect_type(text); + } + template void integer_parser(const std::string& text, T& value) @@ -537,11 +554,14 @@ namespace cxxopts if (negative) { - if (!is_signed) - { - throw argument_incorrect_type(text); - } - value = -result; + value = checked_negate(result, + text, + std::integral_constant()); + //if (!is_signed) + //{ + // throw argument_incorrect_type(text); + //} + //value = -result; } else {