[1/2] optional: add operator== and operator!=

Message ID 20220314181312.3436802-2-gprocida@google.com
State New
Headers
Series Bug 28954 - add Linux Kernel symbol namespace support |

Commit Message

Giuliano Procida March 14, 2022, 6:13 p.m. UTC
  * include/abg-cxx-compat.h (optional): Add operator== and
	operator!=.

Signed-off-by: Giuliano Procida <gprocida@google.com>
---
 include/abg-cxx-compat.h | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
  

Comments

Matthias Männich March 15, 2022, 11:02 a.m. UTC | #1
On Mon, Mar 14, 2022 at 06:13:11PM +0000, Giuliano Procida wrote:
>	* include/abg-cxx-compat.h (optional): Add operator== and
>	operator!=.
>
>Signed-off-by: Giuliano Procida <gprocida@google.com>
>---
> include/abg-cxx-compat.h | 16 ++++++++++++++++
> 1 file changed, 16 insertions(+)
>
>diff --git a/include/abg-cxx-compat.h b/include/abg-cxx-compat.h
>index 443905c7..a2cf9095 100644
>--- a/include/abg-cxx-compat.h
>+++ b/include/abg-cxx-compat.h
>@@ -91,6 +91,22 @@ public:
>   }
>
>   explicit operator bool() const { return has_value_; }

     std signature is (at least make it noexcept):
       > constexpr explicit operator bool() const noexcept;

>+
>+  bool
>+  operator==(const optional<T>& other) const
>+  {
>+    if (!has_value_ && !other.has_value_)
>+      return true;
>+    if (!has_value_ || !other.has_value_)
>+      return false;
>+    return value_ == other.value_;
>+  }
>+
>+  bool
>+  operator!=(const optional<T>& other) const
>+  {
>+    return !operator==(other);
>+  }

I believe C++17 defines those as non-member functions. Should we follow
that for compatibility. Eventually, when switching to C++17 or later we
would want to just replace abg_compat:: with std:: and things should
"just work".

Cheers,
Matthias

> };
>
> #endif
>-- 
>2.35.1.723.g4982287a31-goog
>
  
Giuliano Procida March 16, 2022, 9:31 a.m. UTC | #2
Hi.

On Tue, 15 Mar 2022 at 11:02, Matthias Maennich <maennich@google.com> wrote:
>
> On Mon, Mar 14, 2022 at 06:13:11PM +0000, Giuliano Procida wrote:
> >       * include/abg-cxx-compat.h (optional): Add operator== and
> >       operator!=.
> >
> >Signed-off-by: Giuliano Procida <gprocida@google.com>
> >---
> > include/abg-cxx-compat.h | 16 ++++++++++++++++
> > 1 file changed, 16 insertions(+)
> >
> >diff --git a/include/abg-cxx-compat.h b/include/abg-cxx-compat.h
> >index 443905c7..a2cf9095 100644
> >--- a/include/abg-cxx-compat.h
> >+++ b/include/abg-cxx-compat.h
> >@@ -91,6 +91,22 @@ public:
> >   }
> >
> >   explicit operator bool() const { return has_value_; }
>
>      std signature is (at least make it noexcept):
>        > constexpr explicit operator bool() const noexcept;
>

Will do. I could add some, but not all, the constexpr as C++11
complains about ambiguous overloads.

> >+
> >+  bool
> >+  operator==(const optional<T>& other) const
> >+  {
> >+    if (!has_value_ && !other.has_value_)
> >+      return true;
> >+    if (!has_value_ || !other.has_value_)
> >+      return false;
> >+    return value_ == other.value_;
> >+  }
> >+
> >+  bool
> >+  operator!=(const optional<T>& other) const
> >+  {
> >+    return !operator==(other);
> >+  }
>
> I believe C++17 defines those as non-member functions. Should we follow
> that for compatibility. Eventually, when switching to C++17 or later we
> would want to just replace abg_compat:: with std:: and things should
> "just work".
>

Sure.

> Cheers,
> Matthias
>

v2 will follow.

Giuliano.

> > };
> >
> > #endif
> >--
> >2.35.1.723.g4982287a31-goog
> >
  

Patch

diff --git a/include/abg-cxx-compat.h b/include/abg-cxx-compat.h
index 443905c7..a2cf9095 100644
--- a/include/abg-cxx-compat.h
+++ b/include/abg-cxx-compat.h
@@ -91,6 +91,22 @@  public:
   }
 
   explicit operator bool() const { return has_value_; }
+
+  bool
+  operator==(const optional<T>& other) const
+  {
+    if (!has_value_ && !other.has_value_)
+      return true;
+    if (!has_value_ || !other.has_value_)
+      return false;
+    return value_ == other.value_;
+  }
+
+  bool
+  operator!=(const optional<T>& other) const
+  {
+    return !operator==(other);
+  }
 };
 
 #endif