[libc-commits] [libc] [libc][NFC] Fix accessor qualifiers for `cpp::expected` (PR #80424)

Guillaume Chatelet via libc-commits libc-commits at lists.llvm.org
Fri Feb 2 04:41:11 PST 2024


https://github.com/gchatelet updated https://github.com/llvm/llvm-project/pull/80424

>From e8c924ce45ab8639715a987d6a2b563f975b068f Mon Sep 17 00:00:00 2001
From: Guillaume Chatelet <gchatelet at google.com>
Date: Fri, 2 Feb 2024 12:39:12 +0000
Subject: [PATCH 1/2] [libc][NFC] Fix accessor qualifiers for `cpp::expected`

---
 libc/src/__support/CPP/expected.h | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/libc/src/__support/CPP/expected.h b/libc/src/__support/CPP/expected.h
index 52174a054f874..100abfc221161 100644
--- a/libc/src/__support/CPP/expected.h
+++ b/libc/src/__support/CPP/expected.h
@@ -33,10 +33,12 @@ template <class T, class E> class expected {
   constexpr expected(unexpected<E> unexp)
       : unexp(unexp.error()), is_expected(false) {}
 
-  constexpr bool has_value() { return is_expected; }
+  constexpr bool has_value() const { return is_expected; }
 
-  constexpr T value() { return exp; }
-  constexpr E error() { return unexp; }
+  constexpr T &value() { return exp; }
+  constexpr E &error() { return unexp; }
+  constexpr const T &value() const { return exp; }
+  constexpr const E &error() const { return unexp; }
 
   constexpr operator bool() { return is_expected; }
 

>From 1dda4d8cee4251f315352d40716d52561e41a799 Mon Sep 17 00:00:00 2001
From: Guillaume Chatelet <gchatelet at google.com>
Date: Fri, 2 Feb 2024 12:40:56 +0000
Subject: [PATCH 2/2] Also const qualify cast to bool operator

---
 libc/src/__support/CPP/expected.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/src/__support/CPP/expected.h b/libc/src/__support/CPP/expected.h
index 100abfc221161..9682de981a834 100644
--- a/libc/src/__support/CPP/expected.h
+++ b/libc/src/__support/CPP/expected.h
@@ -40,7 +40,7 @@ template <class T, class E> class expected {
   constexpr const T &value() const { return exp; }
   constexpr const E &error() const { return unexp; }
 
-  constexpr operator bool() { return is_expected; }
+  constexpr operator bool() const { return is_expected; }
 
   constexpr T &operator*() { return exp; }
   constexpr const T &operator*() const { return exp; }



More information about the libc-commits mailing list