Type which are convertible to a number should not participate in arit… (#609)

* Type which are convertible to a number should not participate in arithmetic operator overloads.
Fixes https://github.com/boostorg/multiprecision/issues/608
This commit is contained in:
jzmaddock
2024-03-07 17:23:49 +00:00
committed by GitHub
parent ffe506aacb
commit 2e81e42a0a
3 changed files with 24 additions and 1 deletions

View File

@@ -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 {

View File

@@ -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 : ]

15
test/git_issue_608.cpp Normal file
View File

@@ -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");
}