Skip to content

Commit

Permalink
Merge pull request #852 from cppalliance/delegate
Browse files Browse the repository at this point in the history
Use delegating constructor for Integers
  • Loading branch information
mborland authored Feb 1, 2025
2 parents 037cdbf + 59fdb70 commit 0e0a7ec
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 35 deletions.
4 changes: 1 addition & 3 deletions include/boost/decimal/decimal128.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -960,10 +960,8 @@ template <BOOST_DECIMAL_INTEGRAL Integer>
#else
template <typename Integer, std::enable_if_t<detail::is_integral_v<Integer>, bool>>
#endif
constexpr decimal128::decimal128(Integer val) noexcept // NOLINT : Incorrect parameter is never used
constexpr decimal128::decimal128(Integer val) noexcept : decimal128{val, 0}
{
using ConversionType = std::conditional_t<std::is_same<Integer, bool>::value, std::int32_t, Integer>;
*this = decimal128{static_cast<ConversionType>(val), 0};
}

template <typename Integer>
Expand Down
4 changes: 1 addition & 3 deletions include/boost/decimal/decimal128_fast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,10 +412,8 @@ template <BOOST_DECIMAL_INTEGRAL Integer>
#else
template <typename Integer, std::enable_if_t<detail::is_integral_v<Integer>, bool>>
#endif
constexpr decimal128_fast::decimal128_fast(Integer val) noexcept
constexpr decimal128_fast::decimal128_fast(Integer val) noexcept : decimal128_fast{val, 0}
{
using ConversionType = std::conditional_t<std::is_same<Integer, bool>::value, std::int32_t, Integer>;
*this = decimal128_fast{static_cast<ConversionType>(val), 0, false};
}

#if defined(__clang__)
Expand Down
15 changes: 12 additions & 3 deletions include/boost/decimal/decimal32.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,7 @@ BOOST_DECIMAL_EXPORT class decimal32 final // NOLINT(cppcoreguidelines-special-m
#if defined(__GNUC__) && __GNUC__ >= 6
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wduplicated-branches"
# pragma GCC diagnostic ignored "-Wbool-compare"
#endif

#ifdef BOOST_DECIMAL_HAS_CONCEPTS
Expand All @@ -609,11 +610,21 @@ constexpr decimal32::decimal32(T coeff, T2 exp, bool sign) noexcept // NOLINT(re
Unsigned_Integer unsigned_coeff {detail::make_positive_unsigned(coeff)};
BOOST_DECIMAL_IF_CONSTEXPR (detail::is_signed_v<T>)
{
// This branch will never be taken by bool but it throws a warning prior to C++17
#ifdef _MSC_VER
# pragma warning(push)
# pragma warning(disable : 4804)
#endif

if (coeff < 0 || sign)
{
bits_ |= detail::d32_sign_mask;
isneg = true;
}

#ifdef _MSC_VER
# pragma warning(pop)
#endif
}
else
{
Expand Down Expand Up @@ -1535,10 +1546,8 @@ template <BOOST_DECIMAL_INTEGRAL Integer>
#else
template <typename Integer, std::enable_if_t<detail::is_integral_v<Integer>, bool>>
#endif
constexpr decimal32::decimal32(Integer val) noexcept // NOLINT : Incorrect parameter is never used
constexpr decimal32::decimal32(Integer val) noexcept : decimal32{val, 0}// NOLINT : Incorrect parameter is never used
{
using ConversionType = std::conditional_t<std::is_same<Integer, bool>::value, std::int32_t, Integer>;
*this = decimal32{static_cast<ConversionType>(val), 0};
}

template <typename Integer>
Expand Down
4 changes: 1 addition & 3 deletions include/boost/decimal/decimal32_fast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,10 +393,8 @@ constexpr decimal32_fast::decimal32_fast(T1 coeff, T2 exp, bool sign) noexcept
}

template <typename Integer, std::enable_if_t<detail::is_integral_v<Integer>, bool>>
constexpr decimal32_fast::decimal32_fast(Integer val) noexcept
constexpr decimal32_fast::decimal32_fast(Integer val) noexcept : decimal32_fast{val, 0}
{
using ConversionType = std::conditional_t<std::is_same<Integer, bool>::value, std::int32_t, Integer>;
*this = decimal32_fast{static_cast<ConversionType>(val), 0, false};
}

#if defined(__clang__)
Expand Down
32 changes: 12 additions & 20 deletions include/boost/decimal/decimal64.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,13 +327,6 @@ BOOST_DECIMAL_EXPORT class decimal64 final
#endif
constexpr decimal64(T1 coeff, T2 exp, bool sign = false) noexcept;

#ifdef BOOST_DECIMAL_HAS_CONCEPTS
template <BOOST_DECIMAL_INTEGRAL T>
#else
template <typename T, std::enable_if_t<detail::is_integral_v<T>, bool> = true>
#endif
constexpr decimal64(bool coeff, T exp, bool sign = false) noexcept;

// cmath functions that are easier as friends
friend constexpr auto signbit BOOST_DECIMAL_PREVENT_MACRO_SUBSTITUTION (decimal64 rhs) noexcept -> bool;
friend constexpr auto isnan BOOST_DECIMAL_PREVENT_MACRO_SUBSTITUTION (decimal64 rhs) noexcept -> bool;
Expand Down Expand Up @@ -610,6 +603,7 @@ constexpr auto to_bits(decimal64 rhs) noexcept -> std::uint64_t
#if defined(__GNUC__) && __GNUC__ >= 6
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wduplicated-branches"
# pragma GCC diagnostic ignored "-Wbool-compare"
#endif

// 3.2.5 initialization from coefficient and exponent:
Expand All @@ -627,11 +621,21 @@ constexpr decimal64::decimal64(T1 coeff, T2 exp, bool sign) noexcept
Unsigned_Integer unsigned_coeff {detail::make_positive_unsigned(coeff)};
BOOST_DECIMAL_IF_CONSTEXPR (detail::is_signed_v<T1>)
{
// This branch will never be taken by bool but it throws a warning prior to C++17
#ifdef _MSC_VER
# pragma warning(push)
# pragma warning(disable : 4804)
#endif

if (coeff < 0 || sign)
{
bits_ |= detail::d64_sign_mask;
isneg = true;
}

#ifdef _MSC_VER
# pragma warning(pop)
#endif
}
else
{
Expand Down Expand Up @@ -860,10 +864,8 @@ template <BOOST_DECIMAL_INTEGRAL Integer>
#else
template <typename Integer, std::enable_if_t<detail::is_integral_v<Integer>, bool>>
#endif
constexpr decimal64::decimal64(Integer val) noexcept // NOLINT : Incorrect parameter is never used
constexpr decimal64::decimal64(Integer val) noexcept : decimal64{val, 0}
{
using ConversionType = std::conditional_t<std::is_same<Integer, bool>::value, std::int32_t, Integer>;
*this = decimal64{static_cast<ConversionType>(val), 0};
}

template <typename Integer>
Expand All @@ -875,16 +877,6 @@ constexpr auto decimal64::operator=(const Integer& val) noexcept
return *this;
}

#ifdef BOOST_DECIMAL_HAS_CONCEPTS
template <BOOST_DECIMAL_INTEGRAL T>
#else
template <typename T, std::enable_if_t<detail::is_integral_v<T>, bool>>
#endif
constexpr decimal64::decimal64(bool coeff, T exp, bool sign) noexcept
{
*this = decimal64(static_cast<std::int32_t>(coeff), exp, sign);
}

constexpr decimal64::operator bool() const noexcept
{
constexpr decimal64 zero {0, 0};
Expand Down
4 changes: 1 addition & 3 deletions include/boost/decimal/decimal64_fast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,10 +407,8 @@ template <BOOST_DECIMAL_INTEGRAL Integer>
#else
template <typename Integer, std::enable_if_t<detail::is_integral_v<Integer>, bool>>
#endif
constexpr decimal64_fast::decimal64_fast(Integer val) noexcept
constexpr decimal64_fast::decimal64_fast(Integer val) noexcept : decimal64_fast{val, 0}
{
using ConversionType = std::conditional_t<std::is_same<Integer, bool>::value, std::int32_t, Integer>;
*this = decimal64_fast{static_cast<ConversionType>(val), 0, false};
}

#if defined(__clang__)
Expand Down
3 changes: 3 additions & 0 deletions include/boost/decimal/detail/type_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ constexpr bool is_unsigned_v = !is_signed_v<T>;
template <typename T>
struct make_unsigned { using type = std::make_unsigned_t<T>; };

template <>
struct make_unsigned<bool> { using type = std::uint8_t; };

template <>
struct make_unsigned<uint128> { using type = uint128; };

Expand Down

0 comments on commit 0e0a7ec

Please sign in to comment.