apply_sign.hpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright 2023 Matt Borland
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // https://www.boost.org/LICENSE_1_0.txt
  4. #ifndef BOOST_CHARCONV_DETAIL_APPLY_SIGN_HPP
  5. #define BOOST_CHARCONV_DETAIL_APPLY_SIGN_HPP
  6. #include <boost/config.hpp>
  7. #include <boost/charconv/detail/emulated128.hpp>
  8. #include <boost/charconv/detail/type_traits.hpp>
  9. #include <type_traits>
  10. // We are purposefully converting values here
  11. #ifdef BOOST_MSVC
  12. # pragma warning(push)
  13. # pragma warning(disable: 4146)
  14. #elif defined(__GNUC__) && __GNUC__ >= 5
  15. # pragma GCC diagnostic push
  16. # pragma GCC diagnostic ignored "-Wconversion"
  17. #elif defined(__clang__)
  18. # pragma clang diagnostic push
  19. # pragma clang diagnostic ignored "-Wconversion"
  20. #endif
  21. namespace boost { namespace charconv { namespace detail {
  22. template <typename Integer, typename Unsigned_Integer = detail::make_unsigned_t<Integer>,
  23. typename std::enable_if<detail::is_signed<Integer>::value, bool>::type = true>
  24. constexpr Unsigned_Integer apply_sign(Integer val) noexcept
  25. {
  26. return -(static_cast<Unsigned_Integer>(val));
  27. }
  28. template <typename Unsigned_Integer, typename std::enable_if<!detail::is_signed<Unsigned_Integer>::value, bool>::type = true>
  29. constexpr Unsigned_Integer apply_sign(Unsigned_Integer val) noexcept
  30. {
  31. return val;
  32. }
  33. }}} // Namespaces
  34. #ifdef BOOST_MSVC
  35. # pragma warning(pop)
  36. #elif defined(__GNUC__) && __GNUC__ >= 5
  37. # pragma GCC diagnostic pop
  38. #elif defined(__clang__)
  39. # pragma clang diagnostic pop
  40. #endif
  41. #endif // BOOST_CHARCONV_DETAIL_APPLY_SIGN_HPP