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

Samira Bazuzi via libc-commits libc-commits at lists.llvm.org
Wed Oct 11 07:34:03 PDT 2023


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

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 usage site libc/test/UnitTest/PrintfMatcher.cpp:28.

>From c55805e8f31434bf0cbaff178d378677d4de889c Mon Sep 17 00:00:00 2001
From: Samira Bazuzi <bazuzi at google.com>
Date: Wed, 11 Oct 2023 10:31:39 -0400
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 usage
site libc/test/UnitTest/PrintfMatcher.cpp:28.
---
 libc/src/stdio/printf_core/core_structs.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/src/stdio/printf_core/core_structs.h b/libc/src/stdio/printf_core/core_structs.h
index e96bc4dc0f8dd3c..88a13703fd94761 100644
--- a/libc/src/stdio/printf_core/core_structs.h
+++ b/libc/src/stdio/printf_core/core_structs.h
@@ -53,7 +53,7 @@ struct FormatSection {
 
   // This operator is only used for testing and should be automatically
   // optimized out for release builds.
-  bool operator==(const FormatSection &other) {
+  bool operator==(const FormatSection &other) const {
     if (has_conv != other.has_conv)
       return false;
 



More information about the libc-commits mailing list