Skip to content

Commit 332f08f

Browse files
authored
Merge pull request #1089 from cppalliance/1087
Improve message from disallowed 3 arg constructor
2 parents dc95654 + c04034a commit 332f08f

File tree

8 files changed

+69
-0
lines changed

8 files changed

+69
-0
lines changed

include/boost/decimal/decimal128_t.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,13 @@ BOOST_DECIMAL_EXPORT class decimal128_t final
257257
#endif
258258
constexpr decimal128_t(T1 coeff, T2 exp, bool sign = false) noexcept;
259259

260+
#ifdef BOOST_DECIMAL_HAS_CONCEPTS
261+
template <BOOST_DECIMAL_SIGNED_INTEGRAL T1, BOOST_DECIMAL_INTEGRAL T2>
262+
#else
263+
template <typename T1, typename T2, std::enable_if_t<!detail::is_unsigned_v<T1> && detail::is_integral_v<T2>, bool> = true>
264+
#endif
265+
constexpr decimal128_t(T1, T2, bool) noexcept { static_assert(detail::is_unsigned_v<T1>, "Construction from signed integer, exponent, and sign is ambiguous, so it is disallowed. You must use an Unsigned Integer for the coefficient to construct from {coefficient, exponent, sign}"); }
266+
260267
#ifdef BOOST_DECIMAL_HAS_CONCEPTS
261268
template <BOOST_DECIMAL_SIGNED_INTEGRAL T1, BOOST_DECIMAL_INTEGRAL T2>
262269
#else

include/boost/decimal/decimal32_t.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,13 @@ BOOST_DECIMAL_EXPORT class decimal32_t final // NOLINT(cppcoreguidelines-special
282282
#endif
283283
constexpr decimal32_t(T1 coeff, T2 exp, bool sign = false) noexcept;
284284

285+
#ifdef BOOST_DECIMAL_HAS_CONCEPTS
286+
template <BOOST_DECIMAL_SIGNED_INTEGRAL T1, BOOST_DECIMAL_INTEGRAL T2>
287+
#else
288+
template <typename T1, typename T2, std::enable_if_t<!detail::is_unsigned_v<T1> && detail::is_integral_v<T2>, bool> = true>
289+
#endif
290+
constexpr decimal32_t(T1, T2, bool) noexcept { static_assert(detail::is_unsigned_v<T1>, "Construction from signed integer, exponent, and sign is ambiguous, so it is disallowed. You must use an Unsigned Integer for the coefficient to construct from {coefficient, exponent, sign}"); }
291+
285292
#ifdef BOOST_DECIMAL_HAS_CONCEPTS
286293
template <BOOST_DECIMAL_SIGNED_INTEGRAL T1, BOOST_DECIMAL_INTEGRAL T2>
287294
#else

include/boost/decimal/decimal64_t.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,13 @@ BOOST_DECIMAL_EXPORT class decimal64_t final
319319
#endif
320320
constexpr decimal64_t(T1 coeff, T2 exp, bool sign = false) noexcept;
321321

322+
#ifdef BOOST_DECIMAL_HAS_CONCEPTS
323+
template <BOOST_DECIMAL_SIGNED_INTEGRAL T1, BOOST_DECIMAL_INTEGRAL T2>
324+
#else
325+
template <typename T1, typename T2, std::enable_if_t<!detail::is_unsigned_v<T1> && detail::is_integral_v<T2>, bool> = true>
326+
#endif
327+
constexpr decimal64_t(T1, T2, bool) noexcept { static_assert(detail::is_unsigned_v<T1>, "Construction from signed integer, exponent, and sign is ambiguous, so it is disallowed. You must use an Unsigned Integer for the coefficient to construct from {coefficient, exponent, sign}"); }
328+
322329
#ifdef BOOST_DECIMAL_HAS_CONCEPTS
323330
template <BOOST_DECIMAL_SIGNED_INTEGRAL T1, BOOST_DECIMAL_INTEGRAL T2>
324331
#else

include/boost/decimal/decimal_fast128_t.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,13 @@ BOOST_DECIMAL_EXPORT class decimal_fast128_t final
157157
#endif
158158
constexpr decimal_fast128_t(T1 coeff, T2 exp, bool sign = false) noexcept;
159159

160+
#ifdef BOOST_DECIMAL_HAS_CONCEPTS
161+
template <BOOST_DECIMAL_SIGNED_INTEGRAL T1, BOOST_DECIMAL_INTEGRAL T2>
162+
#else
163+
template <typename T1, typename T2, std::enable_if_t<!detail::is_unsigned_v<T1> && detail::is_integral_v<T2>, bool> = true>
164+
#endif
165+
constexpr decimal_fast128_t(T1, T2, bool) noexcept { static_assert(detail::is_unsigned_v<T1>, "Construction from signed integer, exponent, and sign is ambiguous, so it is disallowed. You must use an Unsigned Integer for the coefficient to construct from {coefficient, exponent, sign}"); }
166+
160167
#ifdef BOOST_DECIMAL_HAS_CONCEPTS
161168
template <BOOST_DECIMAL_SIGNED_INTEGRAL T1, BOOST_DECIMAL_INTEGRAL T2>
162169
#else

include/boost/decimal/decimal_fast32_t.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ BOOST_DECIMAL_EXPORT class decimal_fast32_t final
150150
template <typename T1, typename T2, std::enable_if_t<detail::is_unsigned_v<T1> && detail::is_integral_v<T2>, bool> = true>
151151
constexpr decimal_fast32_t(T1 coeff, T2 exp, bool sign = false) noexcept;
152152

153+
template <typename T1, typename T2, std::enable_if_t<!detail::is_unsigned_v<T1> && detail::is_integral_v<T2>, bool> = true>
154+
constexpr decimal_fast32_t(T1, T2, bool) noexcept { static_assert(detail::is_unsigned_v<T1>, "Construction from signed integer, exponent, and sign is ambiguous, so it is disallowed. You must use an Unsigned Integer for the coefficient to construct from {coefficient, exponent, sign}"); }
155+
153156
template <typename T1, typename T2, std::enable_if_t<!detail::is_unsigned_v<T1> && detail::is_integral_v<T2>, bool> = true>
154157
constexpr decimal_fast32_t(T1 coeff, T2 exp) noexcept;
155158

include/boost/decimal/decimal_fast64_t.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,13 @@ BOOST_DECIMAL_EXPORT class decimal_fast64_t final
168168
#endif
169169
constexpr decimal_fast64_t(T1 coeff, T2 exp, bool sign = false) noexcept;
170170

171+
#ifdef BOOST_DECIMAL_HAS_CONCEPTS
172+
template <BOOST_DECIMAL_SIGNED_INTEGRAL T1, BOOST_DECIMAL_INTEGRAL T2>
173+
#else
174+
template <typename T1, typename T2, std::enable_if_t<!detail::is_unsigned_v<T1> && detail::is_integral_v<T2>, bool> = true>
175+
#endif
176+
constexpr decimal_fast64_t(T1, T2, bool) noexcept { static_assert(detail::is_unsigned_v<T1>, "Construction from signed integer, exponent, and sign is ambiguous, so it is disallowed. You must use an Unsigned Integer for the coefficient to construct from {coefficient, exponent, sign}"); }
177+
171178
#ifdef BOOST_DECIMAL_HAS_CONCEPTS
172179
template <BOOST_DECIMAL_SIGNED_INTEGRAL T1, BOOST_DECIMAL_INTEGRAL T2>
173180
#else

test/Jamfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ run github_issue_1035.cpp ;
6666
run github_issue_1054.cpp ;
6767
run github_issue_1055.cpp ;
6868
run github_issue_1057.cpp ;
69+
compile-fail github_issue_1087.cpp ;
6970
run link_1.cpp link_2.cpp link_3.cpp ;
7071
run quick.cpp ;
7172
run random_decimal32_comp.cpp ;

test/github_issue_1087.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2025 Matt Borland
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// https://www.boost.org/LICENSE_1_0.txt
4+
//
5+
// See: https://github.com/cppalliance/decimal/issues/1087
6+
7+
#include <boost/decimal.hpp>
8+
#include <boost/core/lightweight_test.hpp>
9+
10+
using namespace boost::decimal;
11+
12+
template <typename T>
13+
void test()
14+
{
15+
T val {-3, 3, true};
16+
BOOST_TEST_EQ(val, T(UINT32_C(3), 3, true));
17+
}
18+
19+
int main()
20+
{
21+
test<decimal32_t>();
22+
test<decimal64_t>();
23+
test<decimal128_t>();
24+
25+
test<decimal_fast32_t>();
26+
test<decimal_fast64_t>();
27+
test<decimal_fast128_t>();
28+
29+
return 0;
30+
}

0 commit comments

Comments
 (0)