Skip to content

[libc] Mark operator== const to avoid ambiguity in C++20. #73954

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 30, 2023

Conversation

bazuzi
Copy link
Contributor

@bazuzi bazuzi commented Nov 30, 2023

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.

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.
@llvmbot llvmbot added the libc label Nov 30, 2023
@llvmbot
Copy link
Member

llvmbot commented Nov 30, 2023

@llvm/pr-subscribers-libc

Author: Samira Bazuzi (bazuzi)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/73954.diff

1 Files Affected:

  • (modified) libc/src/__support/CPP/bitset.h (+2-1)
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;

@bazuzi
Copy link
Contributor Author

bazuzi commented Nov 30, 2023

@gchatelet Could you review this change?

I don't have write access, so if you could also merge when it's ready, I'd appreciate it.

@gchatelet
Copy link
Contributor

LGTM

@gchatelet gchatelet merged commit 3f505cd into llvm:main Nov 30, 2023
@bazuzi bazuzi deleted the const branch November 30, 2023 18:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants