Rewrite gdb_mpz::operator==

Simon pointed out that the recent changes to gdb_mpz caused a build
failure on amd64 macOS.  It turns out to be somewhat difficult to
overload a method in a way that will work "naturally" for all integer
types; especially in a case like gdb_mpz::operator==, where it's
desirable to special case all integer types that are no wider than
'long'.

After a false start, I came up with this patch, which seems to work.
It applies the desirable GMP special cases directly in the body,
rather than via overloads.

Approved-By: Simon Marchi <simon.marchi@efficios.com>
This commit is contained in:
Tom Tromey 2023-04-17 12:59:57 -06:00
parent 19e012c813
commit 3ad6c165ca

View File

@ -323,31 +323,25 @@ struct gdb_mpz
return mpz_cmp_si (m_val, other) < 0;
}
bool operator== (int other) const
/* We want an operator== that can handle all integer types. For
types that are 'long' or narrower, we can use a GMP function and
avoid boxing the RHS. But, because overloading based on integer
type is a pain in C++, we accept all such types here and check
the size in the body. */
template<typename T, typename = gdb::Requires<std::is_integral<T>>>
bool operator== (T other) const
{
return mpz_cmp_si (m_val, other) == 0;
}
bool operator== (long other) const
{
return mpz_cmp_si (m_val, other) == 0;
}
bool operator== (unsigned long other) const
{
return mpz_cmp_ui (m_val, other) == 0;
}
template<typename T,
typename = gdb::Requires<
gdb::And<std::is_integral<T>,
std::integral_constant<bool,
(sizeof (T) > sizeof (long))>>
>
>
bool operator== (T src)
{
return *this == gdb_mpz (src);
if (std::is_signed<T>::value)
{
if (sizeof (T) <= sizeof (long))
return mpz_cmp_si (m_val, other) == 0;
}
else
{
if (sizeof (T) <= sizeof (unsigned long))
return mpz_cmp_ui (m_val, other) == 0;
}
return *this == gdb_mpz (other);
}
bool operator== (const gdb_mpz &other) const