[libc-commits] [libc] [libc] Mark operator== const to avoid ambiguity in C++20. (PR #73954)

Samira Bazuzi via libc-commits libc-commits at lists.llvm.org
Thu Nov 30 07:57:09 PST 2023


https://github.com/bazuzi created https://github.com/llvm/llvm-project/pull/73954

C++20 will automatically generate an operator== with reversed operand order, which is ambiguous with the written operator== when one argument is marked const and the other isn't.

This operator currently triggers -Wambiguous-reversed-operator at several usage sites in libc/test/src/__support/CPP/bitset_test.cpp, starting with line 153.

>From 8593a6ba05ba974fcb128a3ce772e829389f490c Mon Sep 17 00:00:00 2001
From: Samira Bazuzi <bazuzi at google.com>
Date: Thu, 30 Nov 2023 10:46:22 -0500
Subject: [PATCH] [libc] Mark operator== const to avoid ambiguity in C++20.

C++20 will automatically generate an operator== with reversed operand order, which is ambiguous with the written operator== when one argument is marked const and the other isn't.

This operator currently triggers -Wambiguous-reversed-operator at
several usage sites in libc/test/src/__support/CPP/bitset_test.cpp, starting with line 153.
---
 libc/src/__support/CPP/bitset.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/libc/src/__support/CPP/bitset.h b/libc/src/__support/CPP/bitset.h
index cfac749e0d84e3a..30a7fa796cb4b36 100644
--- a/libc/src/__support/CPP/bitset.h
+++ b/libc/src/__support/CPP/bitset.h
@@ -65,7 +65,8 @@ template <size_t NumberOfBits> struct bitset {
     }
   }
 
-  LIBC_INLINE constexpr bool operator==(const bitset<NumberOfBits> &other) {
+  LIBC_INLINE constexpr bool
+  operator==(const bitset<NumberOfBits> &other) const {
     for (size_t i = 0; i < NUMBER_OF_UNITS; ++i) {
       if (Data[i] != other.Data[i])
         return false;



More information about the libc-commits mailing list