From 3ad6c165ca098a0fa4491893a75b4b1581698484 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Mon, 17 Apr 2023 12:59:57 -0600 Subject: [PATCH] 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 --- gdb/gmp-utils.h | 44 +++++++++++++++++++------------------------- 1 file changed, 19 insertions(+), 25 deletions(-) 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 -- 2.30.2