From: Tom Tromey Date: Mon, 17 Apr 2023 18:59:57 +0000 (-0600) Subject: Rewrite gdb_mpz::operator== X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=3ad6c165ca098a0fa4491893a75b4b1581698484;p=binutils-gdb.git 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 --- diff --git a/gdb/gmp-utils.h b/gdb/gmp-utils.h index d05c11ecbe4..a5c27feb59f 100644 --- a/gdb/gmp-utils.h +++ b/gdb/gmp-utils.h @@ -323,31 +323,25 @@ struct gdb_mpz return mpz_cmp_si (m_val, other) < 0; } - bool operator== (int 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, - std::integral_constant sizeof (long))>> - > - > - bool operator== (T src) - { - return *this == gdb_mpz (src); + /* 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>> + bool operator== (T other) const + { + if (std::is_signed::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