Workaround for warning C4146

This commit is contained in:
Matt Borland
2023-01-20 08:48:35 -08:00
parent 6b41bb6e69
commit 302e9f5f09
3 changed files with 33 additions and 14 deletions

View File

@@ -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 <type_traits>
namespace boost { namespace charconv { namespace detail {
template <typename Integer, typename std::enable_if<std::is_signed<Integer>::value, bool>::type = true>
constexpr Integer apply_sign(Integer val) noexcept
{
return -val;
}
template <typename Integer, typename std::enable_if<std::is_unsigned<Integer>::value, bool>::type = true>
constexpr Integer apply_sign(Integer val) noexcept
{
return val;
}
}}} // Namespaces
#endif // BOOST_CHARCONV_DETAIL_APPLY_SIGN_HPP

View File

@@ -6,6 +6,7 @@
#ifndef BOOST_CHARCONV_FROM_CHARS_HPP_INCLUDED
#define BOOST_CHARCONV_FROM_CHARS_HPP_INCLUDED
#include <boost/charconv/detail/apply_sign.hpp>
#include <boost/charconv/config.hpp>
#include <boost/config.hpp>
#include <type_traits>
@@ -67,18 +68,6 @@ constexpr unsigned char digit_from_char(char val) noexcept
return uchar_values[static_cast<std::size_t>(val)];
}
template <typename Integer, typename std::enable_if<std::is_signed<Integer>::value, bool>::type = true>
constexpr Integer apply_sign(Integer val) noexcept
{
return -val;
}
template <typename Integer, typename std::enable_if<std::is_unsigned<Integer>::value, bool>::type = true>
constexpr Integer apply_sign(Integer val) noexcept
{
return val;
}
template <typename Integer>
BOOST_CXX14_CONSTEXPR boost::charconv::from_chars_result from_chars_integer_impl(const char* first, const char* last, Integer& value, int base) noexcept
{

View File

@@ -6,6 +6,7 @@
#ifndef BOOST_CHARCONV_TO_CHARS_HPP_INCLUDED
#define BOOST_CHARCONV_TO_CHARS_HPP_INCLUDED
#include <boost/charconv/detail/apply_sign.hpp>
#include <boost/charconv/detail/integer_search_trees.hpp>
#include <boost/charconv/config.hpp>
#include <type_traits>
@@ -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<Integer>::digits <= std::numeric_limits<std::uint32_t>::digits ||
value <= static_cast<Integer>((std::numeric_limits<std::uint32_t>::max)()))