Skip to content

Commit

Permalink
Type which are convertible to a number should not participate in arit… (
Browse files Browse the repository at this point in the history
#609)

* Type which are convertible to a number should not participate in arithmetic operator overloads.
Fixes #608
  • Loading branch information
jzmaddock committed Mar 7, 2024
1 parent ffe506a commit 2e81e42
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
9 changes: 8 additions & 1 deletion include/boost/multiprecision/detail/number_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,17 @@ template <class tag, class Arg1, class Arg2, class Arg3, class Arg4>
struct is_number_expression<detail::expression<tag, Arg1, Arg2, Arg3, Arg4> > : public std::integral_constant<bool, true>
{};

namespace detail {
template <class Val, class Backend>
struct canonical;
}

template <class T, class Num>
struct is_compatible_arithmetic_type
: public std::integral_constant<bool,
std::is_convertible<T, Num>::value && !std::is_same<T, Num>::value && !is_number_expression<T>::value>
std::is_convertible<T, Num>::value && !std::is_same<T, Num>::value && !is_number_expression<T>::value
&& (std::is_constructible<typename Num::backend_type, typename detail::canonical<T, typename Num::backend_type>::type>::value
|| std::is_assignable<typename Num::backend_type, typename detail::canonical<T, typename Num::backend_type>::type>::value || is_number<T>::value || is_number_expression<T>::value)>
{};

namespace detail {
Expand Down
1 change: 1 addition & 0 deletions test/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -1240,6 +1240,7 @@ test-suite misc :
[ check-target-builds ../config//has_float128 : <source>quadmath <define>TEST_FLOAT128 ]
<define>TEST_CPP_DEC_FLOAT
<define>TEST_CPP_BIN_FLOAT ]
[ compile git_issue_608.cpp ]
[ compile git_issue_98.cpp :
[ check-target-builds ../config//has_float128 : <define>TEST_FLOAT128 <source>quadmath : ]
[ check-target-builds ../config//has_gmp : <define>TEST_GMP <source>gmp : ]
Expand Down
15 changes: 15 additions & 0 deletions test/git_issue_608.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright 2024 John Maddock. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include <functional>
#include <boost/multiprecision/cpp_bin_float.hpp>

using big_float_type = boost::multiprecision::cpp_bin_float_100;

int main()
{
static_assert(boost::multiprecision::is_compatible_arithmetic_type<std::reference_wrapper<big_float_type>, big_float_type>::value == 0, "This should not be a compatible type");
}

0 comments on commit 2e81e42

Please sign in to comment.