Skip to content

Commit 519dede

Browse files
authored
Merge pull request #1131 from cppalliance/1116
Make construction from integral types explicit by default
2 parents 0344048 + 72ae1e9 commit 519dede

File tree

12 files changed

+52
-7
lines changed

12 files changed

+52
-7
lines changed

doc/modules/ROOT/pages/api_reference.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ enums, constants and macros that are provided in this library.
5151
| xref:config.adoc#configuration_user[`BOOST_DECIMAL_DISABLE_IOSTREAM`] | xref:config.adoc#configuration_automatic[`BOOST_DECIMAL_NO_CONSTEVAL_DETECTION`]
5252
| xref:config.adoc#configuration_user[`BOOST_DECIMAL_DISABLE_CLIB`] | xref:config.adoc#configuration_automatic[`BOOST_DECIMAL_HAS_STD_CHARCONV`]
5353
| xref:config.adoc#configuration_user[`BOOST_DECIMAL_DISABLE_EXCEPTIONS`] | xref:config.adoc#configuration_automatic[`BOOST_DECIMAL_HAS_STD_STRING_VIEW`]
54+
| xref:config.adoc#configuration_user[`BOOST_DECIMAL_ALLOW_IMPLICIT_INTEGER_CONVERSIONS`] |
55+
| xref:config.adoc#configuration_user[`BOOST_DECIMAL_ALLOW_IMPLICIT_FLOAT_CONVERSIONS`] |
5456
| xref:config.adoc#configuration_user[`BOOST_DECIMAL_ALLOW_IMPLICIT_CONVERSIONS`] |
5557
| xref:config.adoc#configuration_user[`BOOST_DECIMAL_FAST_MATH`] |
5658
| xref:config.adoc#configuration_user[`BOOST_DECIMAL_DEC_EVAL_METHOD`] |

doc/modules/ROOT/pages/config.adoc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ The following configuration macros are available:
2222
- `BOOST_DECIMAL_DISABLE_EXCEPTIONS`: This allows the user to disable any calls to `throw`.
2323
This macro will also be set automatically in the presence of `-fno-exceptions` or various `/EH` flags on MSVC.
2424
25-
- `BOOST_DECIMAL_ALLOW_IMPLICIT_CONVERSIONS`: Allows a binary floating-point type (e.g. `double`) to be implicitly converted to a decimal floating point type.
25+
- `BOOST_DECIMAL_ALLOW_IMPLICIT_INTEGER_CONVERSIONS`: Allows any integer type to be implicitly converted to any decimal type.
26+
27+
- `BOOST_DECIMAL_ALLOW_IMPLICIT_FLOAT_CONVERSIONS`: Allows a binary floating-point type (e.g. `double`) to be implicitly converted to a decimal floating point type.
2628
This option is not recommended, but can be useful if you want to use specific functionality from the standard library with internal conversions such as:
2729
2830
[source, c++]
@@ -36,6 +38,8 @@ std::complex<decimal64_t> test_val {half, half};
3638
const auto res = std::acos(test_val);
3739
----
3840

41+
- `BOOST_DECIMAL_ALLOW_IMPLICIT_CONVERSIONS`: Defines both `BOOST_DECIMAL_ALLOW_IMPLICIT_INTEGER_CONVERSIONS` and `BOOST_DECIMAL_ALLOW_IMPLICIT_FLOAT_CONVERSIONS`
42+
3943
- `BOOST_DECIMAL_FAST_MATH` performs optimizations similar to that of the `-ffast-math` compiler flag such as removing all checks for non-finite values.
4044
This flag increases the performance of the basis operations (e.g. add, sub, mul, div, and comparisons) by up to 20%.
4145
Again, it must be defined before inclusion of decimal headers like so:

examples/statistics.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// Distributed under the Boost Software License, Version 1.0.
33
// https://www.boost.org/LICENSE_1_0.txt
44

5+
// Needed for operations with boost math
6+
#define BOOST_DECIMAL_ALLOW_IMPLICIT_INTEGER_CONVERSIONS
7+
58
#include "where_file.hpp"
69
#include <boost/decimal.hpp>
710
#include <iostream>

include/boost/decimal/decimal128_t.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ BOOST_DECIMAL_EXPORT class decimal128_t final
218218
#else
219219
template <typename Float, std::enable_if_t<detail::is_floating_point_v<Float>, bool> = true>
220220
#endif
221-
#ifndef BOOST_DECIMAL_ALLOW_IMPLICIT_CONVERSIONS
221+
#if !defined(BOOST_DECIMAL_ALLOW_IMPLICIT_CONVERSIONS) && !defined(BOOST_DECIMAL_ALLOW_IMPLICIT_FLOAT_CONVERSIONS)
222222
explicit
223223
#endif
224224
BOOST_DECIMAL_CXX20_CONSTEXPR decimal128_t(Float val) noexcept;
@@ -243,6 +243,9 @@ BOOST_DECIMAL_EXPORT class decimal128_t final
243243
#else
244244
template <typename Integer, std::enable_if_t<detail::is_integral_v<Integer>, bool> = true>
245245
#endif
246+
#if !defined(BOOST_DECIMAL_ALLOW_IMPLICIT_CONVERSIONS) && !defined(BOOST_DECIMAL_ALLOW_IMPLICIT_INTEGER_CONVERSIONS)
247+
explicit
248+
#endif
246249
constexpr decimal128_t(Integer val) noexcept;
247250

248251
template <typename Integer>

include/boost/decimal/decimal32_t.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ BOOST_DECIMAL_EXPORT class decimal32_t final // NOLINT(cppcoreguidelines-special
224224
#else
225225
template <typename Float, std::enable_if_t<detail::is_floating_point_v<Float>, bool> = true>
226226
#endif
227-
#ifndef BOOST_DECIMAL_ALLOW_IMPLICIT_CONVERSIONS
227+
#if !defined(BOOST_DECIMAL_ALLOW_IMPLICIT_CONVERSIONS) && !defined(BOOST_DECIMAL_ALLOW_IMPLICIT_FLOAT_CONVERSIONS)
228228
explicit
229229
#endif
230230
BOOST_DECIMAL_CXX20_CONSTEXPR decimal32_t(Float val) noexcept;
@@ -250,6 +250,9 @@ BOOST_DECIMAL_EXPORT class decimal32_t final // NOLINT(cppcoreguidelines-special
250250
#else
251251
template <BOOST_DECIMAL_INTEGRAL Integer, std::enable_if_t<detail::is_integral_v<Integer>, bool> = true>
252252
#endif
253+
#if !defined(BOOST_DECIMAL_ALLOW_IMPLICIT_CONVERSIONS) && !defined(BOOST_DECIMAL_ALLOW_IMPLICIT_INTEGER_CONVERSIONS)
254+
explicit
255+
#endif
253256
constexpr decimal32_t(Integer val) noexcept;
254257

255258
template <typename Integer>

include/boost/decimal/decimal64_t.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ BOOST_DECIMAL_EXPORT class decimal64_t final
236236
#else
237237
template <typename Float, std::enable_if_t<detail::is_floating_point_v<Float>, bool> = true>
238238
#endif
239-
#ifndef BOOST_DECIMAL_ALLOW_IMPLICIT_CONVERSIONS
239+
#if !defined(BOOST_DECIMAL_ALLOW_IMPLICIT_CONVERSIONS) && !defined(BOOST_DECIMAL_ALLOW_IMPLICIT_FLOAT_CONVERSIONS)
240240
explicit
241241
#endif
242242
BOOST_DECIMAL_CXX20_CONSTEXPR decimal64_t(Float val) noexcept;
@@ -262,6 +262,9 @@ BOOST_DECIMAL_EXPORT class decimal64_t final
262262
#else
263263
template <typename Integer, std::enable_if_t<detail::is_integral_v<Integer>, bool> = true>
264264
#endif
265+
#if !defined(BOOST_DECIMAL_ALLOW_IMPLICIT_CONVERSIONS) && !defined(BOOST_DECIMAL_ALLOW_IMPLICIT_INTEGER_CONVERSIONS)
266+
explicit
267+
#endif
265268
constexpr decimal64_t(Integer val) noexcept;
266269

267270
template <typename Integer>

include/boost/decimal/decimal_fast128_t.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,20 @@ BOOST_DECIMAL_EXPORT class decimal_fast128_t final
183183
#else
184184
template <typename Integer, std::enable_if_t<detail::is_integral_v<Integer>, bool> = true>
185185
#endif
186+
#if !defined(BOOST_DECIMAL_ALLOW_IMPLICIT_CONVERSIONS) && !defined(BOOST_DECIMAL_ALLOW_IMPLICIT_INTEGER_CONVERSIONS)
187+
explicit
188+
#endif
186189
constexpr decimal_fast128_t(Integer val) noexcept;
187190

188191
#ifdef BOOST_DECIMAL_HAS_CONCEPTS
189192
template <BOOST_DECIMAL_REAL Float>
190193
#else
191194
template <typename Float, std::enable_if_t<detail::is_floating_point_v<Float>, bool> = true>
192195
#endif
193-
explicit BOOST_DECIMAL_CXX20_CONSTEXPR decimal_fast128_t(Float val) noexcept;
196+
#if !defined(BOOST_DECIMAL_ALLOW_IMPLICIT_CONVERSIONS) && !defined(BOOST_DECIMAL_ALLOW_IMPLICIT_FLOAT_CONVERSIONS)
197+
explicit
198+
#endif
199+
BOOST_DECIMAL_CXX20_CONSTEXPR decimal_fast128_t(Float val) noexcept;
194200

195201
#ifdef BOOST_DECIMAL_UNSUPPORTED_LONG_DOUBLE
196202
explicit BOOST_DECIMAL_CXX20_CONSTEXPR decimal_fast128_t(long double val) noexcept = delete;

include/boost/decimal/decimal_fast32_t.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,16 @@ BOOST_DECIMAL_EXPORT class decimal_fast32_t final
159159
explicit constexpr decimal_fast32_t(bool value) noexcept;
160160

161161
template <typename Integer, std::enable_if_t<detail::is_integral_v<Integer>, bool> = true>
162+
#if !defined(BOOST_DECIMAL_ALLOW_IMPLICIT_CONVERSIONS) && !defined(BOOST_DECIMAL_ALLOW_IMPLICIT_INTEGER_CONVERSIONS)
163+
explicit
164+
#endif
162165
constexpr decimal_fast32_t(Integer coeff) noexcept;
163166

164167
template <typename Float, std::enable_if_t<detail::is_floating_point_v<Float>, bool> = true>
165-
explicit BOOST_DECIMAL_CXX20_CONSTEXPR decimal_fast32_t(Float val) noexcept;
168+
#if !defined(BOOST_DECIMAL_ALLOW_IMPLICIT_CONVERSIONS) && !defined(BOOST_DECIMAL_ALLOW_IMPLICIT_FLOAT_CONVERSIONS)
169+
explicit
170+
#endif
171+
BOOST_DECIMAL_CXX20_CONSTEXPR decimal_fast32_t(Float val) noexcept;
166172

167173
#ifdef BOOST_DECIMAL_UNSUPPORTED_LONG_DOUBLE
168174
explicit constexpr decimal_fast32_t(long double val) noexcept = delete;

include/boost/decimal/decimal_fast64_t.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,14 +189,20 @@ BOOST_DECIMAL_EXPORT class decimal_fast64_t final
189189
#else
190190
template <typename Integer, std::enable_if_t<detail::is_integral_v<Integer>, bool> = true>
191191
#endif
192+
#if !defined(BOOST_DECIMAL_ALLOW_IMPLICIT_CONVERSIONS) && !defined(BOOST_DECIMAL_ALLOW_IMPLICIT_INTEGER_CONVERSIONS)
193+
explicit
194+
#endif
192195
constexpr decimal_fast64_t(Integer val) noexcept;
193196

194197
#ifdef BOOST_DECIMAL_HAS_CONCEPTS
195198
template <BOOST_DECIMAL_REAL Float>
196199
#else
197200
template <typename Float, std::enable_if_t<detail::is_floating_point_v<Float>, bool> = true>
198201
#endif
199-
explicit BOOST_DECIMAL_CXX20_CONSTEXPR decimal_fast64_t(Float val) noexcept;
202+
#if !defined(BOOST_DECIMAL_ALLOW_IMPLICIT_CONVERSIONS) && !defined(BOOST_DECIMAL_ALLOW_IMPLICIT_FLOAT_CONVERSIONS)
203+
explicit
204+
#endif
205+
BOOST_DECIMAL_CXX20_CONSTEXPR decimal_fast64_t(Float val) noexcept;
200206

201207
#ifdef BOOST_DECIMAL_UNSUPPORTED_LONG_DOUBLE
202208
explicit constexpr decimal_fast64_t(long double val) noexcept = delete;

test/test_boost_math_univariate_stats.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
// Propogates up from boost.math
1010
#define _SILENCE_CXX23_DENORM_DEPRECATION_WARNING
1111

12+
// Needed for operations with boost math
13+
#define BOOST_DECIMAL_ALLOW_IMPLICIT_INTEGER_CONVERSIONS
14+
1215
#include <boost/decimal.hpp>
1316

1417
#if defined(__clang__)

0 commit comments

Comments
 (0)