From 302e9f5f09fbd5a4bc3f1eeab12c8b45d325cb0c Mon Sep 17 00:00:00 2001 From: Matt Borland Date: Fri, 20 Jan 2023 08:48:35 -0800 Subject: [PATCH] Workaround for warning C4146 --- include/boost/charconv/detail/apply_sign.hpp | 29 ++++++++++++++++++++ include/boost/charconv/from_chars.hpp | 13 +-------- include/boost/charconv/to_chars.hpp | 5 ++-- 3 files changed, 33 insertions(+), 14 deletions(-) create mode 100644 include/boost/charconv/detail/apply_sign.hpp diff --git a/include/boost/charconv/detail/apply_sign.hpp b/include/boost/charconv/detail/apply_sign.hpp new file mode 100644 index 0000000..244c0bb --- /dev/null +++ b/include/boost/charconv/detail/apply_sign.hpp @@ -0,0 +1,29 @@ +// Copyright 2023 Matt Borland +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#ifndef BOOST_CHARCONV_DETAIL_APPLY_SIGN_HPP +#define BOOST_CHARCONV_DETAIL_APPLY_SIGN_HPP + +// Workaround for warning C4146: unary minus operator applied to unsigned type, result still unsigned +// Occurs using MSVC with pre-C++17 language standards + +#include + +namespace boost { namespace charconv { namespace detail { + +template ::value, bool>::type = true> +constexpr Integer apply_sign(Integer val) noexcept +{ + return -val; +} + +template ::value, bool>::type = true> +constexpr Integer apply_sign(Integer val) noexcept +{ + return val; +} + +}}} // Namespaces + +#endif // BOOST_CHARCONV_DETAIL_APPLY_SIGN_HPP diff --git a/include/boost/charconv/from_chars.hpp b/include/boost/charconv/from_chars.hpp index 9e5fcc6..4f8f040 100644 --- a/include/boost/charconv/from_chars.hpp +++ b/include/boost/charconv/from_chars.hpp @@ -6,6 +6,7 @@ #ifndef BOOST_CHARCONV_FROM_CHARS_HPP_INCLUDED #define BOOST_CHARCONV_FROM_CHARS_HPP_INCLUDED +#include #include #include #include @@ -67,18 +68,6 @@ constexpr unsigned char digit_from_char(char val) noexcept return uchar_values[static_cast(val)]; } -template ::value, bool>::type = true> -constexpr Integer apply_sign(Integer val) noexcept -{ - return -val; -} - -template ::value, bool>::type = true> -constexpr Integer apply_sign(Integer val) noexcept -{ - return val; -} - template BOOST_CXX14_CONSTEXPR boost::charconv::from_chars_result from_chars_integer_impl(const char* first, const char* last, Integer& value, int base) noexcept { diff --git a/include/boost/charconv/to_chars.hpp b/include/boost/charconv/to_chars.hpp index b4e2cf6..1503c29 100644 --- a/include/boost/charconv/to_chars.hpp +++ b/include/boost/charconv/to_chars.hpp @@ -6,6 +6,7 @@ #ifndef BOOST_CHARCONV_TO_CHARS_HPP_INCLUDED #define BOOST_CHARCONV_TO_CHARS_HPP_INCLUDED +#include #include #include #include @@ -109,7 +110,7 @@ BOOST_CXX14_CONSTEXPR to_chars_result to_chars_integer_impl(char* first, char* l { if (value < 0) { - value = -value; + value = apply_sign(value); is_negative = true; } } @@ -119,7 +120,7 @@ BOOST_CXX14_CONSTEXPR to_chars_result to_chars_integer_impl(char* first, char* l // are present and then decompose the value into two (or more) std::uint32_t of known length so that we // don't have the issue of removing leading zeros from the least significant digits - // Yields: warning C4127: conditional expression is constant becuase first half the expression is constant + // Yields: warning C4127: conditional expression is constant becuase first half of the expression is constant // but we need to short circuit to avoid UB on the second half if (std::numeric_limits::digits <= std::numeric_limits::digits || value <= static_cast((std::numeric_limits::max)()))