diff mbox

std::experimental::optional operator!=

Message ID 20140425193539.GK928@redhat.com
State New
Headers show

Commit Message

Jonathan Wakely April 25, 2014, 7:35 p.m. UTC
On 30/03/14 12:54 +0200, Lars Gullik Bjønnes wrote:
>When trying to convert some code using boost::optional to using
>std::experimental::optional instead if come over the issue that I had to
>implement operator!= for the contained types.
>
>When looking at n3793 it states that operator!= should be implemented
>with !(t1 == t2), and not t1 != t2 as the implementation in gcc 4.9 is
>doing. This is the case for both the operator!= implementation where
>optional<T> is compared against T.
>
>The other operators look ok, and only operator== and operator< are used
>in their implementations.
>
>Can this be fixed before 4.9?

I've committed the patch from PR 60710 (attached) and will apply it to
the 4.9 branch too.

Tested x86_64-linux.
diff mbox

Patch

commit ca00614d7bca5305905073dd558c6efacfb029ad
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Fri Apr 25 20:16:05 2014 +0100

    2014-04-25  Lars Gullik Bjønnes  <larsbj@gullik.org>
    
    	PR libstdc++/60710
    	* include/experimental/optional (operator!=): Implement in terms of
    	operator==.
    	* testsuite/experimental/optional/relops/1.cc: Remove operator!=.
    	* testsuite/experimental/optional/relops/2.cc: Likewise.
    	* testsuite/experimental/optional/relops/3.cc: Likewise.
    	* testsuite/experimental/optional/relops/4.cc: Likewise.
    	* testsuite/experimental/optional/relops/5.cc: Likewise.
    	* testsuite/experimental/optional/relops/6.cc: Likewise.

diff --git a/libstdc++-v3/include/experimental/optional b/libstdc++-v3/include/experimental/optional
index 5f2d93f..2a3f29d 100644
--- a/libstdc++-v3/include/experimental/optional
+++ b/libstdc++-v3/include/experimental/optional
@@ -736,12 +736,12 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
   template<typename _Tp>
     constexpr bool
     operator!=(const optional<_Tp>& __lhs, _Tp const& __rhs)
-    { return !__lhs || *__lhs != __rhs; }
+    { return !__lhs || !(*__lhs == __rhs); }
 
   template<typename _Tp>
     constexpr bool
     operator!=(const _Tp& __lhs, const optional<_Tp>& __rhs)
-    { return !__rhs || __lhs != *__rhs; }
+    { return !__rhs || !(__lhs == *__rhs); }
 
   template<typename _Tp>
     constexpr bool
diff --git a/libstdc++-v3/testsuite/experimental/optional/relops/1.cc b/libstdc++-v3/testsuite/experimental/optional/relops/1.cc
index f140880..3f1ee9c 100644
--- a/libstdc++-v3/testsuite/experimental/optional/relops/1.cc
+++ b/libstdc++-v3/testsuite/experimental/optional/relops/1.cc
@@ -37,10 +37,6 @@  namespace ns
   { return std::tie(lhs.i, lhs.s) == std::tie(rhs.i, rhs.s); }
 
   bool
-  operator!=(value_type const& lhs, value_type const& rhs)
-  { return !(lhs == rhs); }
-
-  bool
   operator<(value_type const& lhs, value_type const& rhs)
   { return std::tie(lhs.i, lhs.s) < std::tie(rhs.i, rhs.s); }
 
diff --git a/libstdc++-v3/testsuite/experimental/optional/relops/2.cc b/libstdc++-v3/testsuite/experimental/optional/relops/2.cc
index c7fc848..6ee9cba 100644
--- a/libstdc++-v3/testsuite/experimental/optional/relops/2.cc
+++ b/libstdc++-v3/testsuite/experimental/optional/relops/2.cc
@@ -37,10 +37,6 @@  namespace ns
   { return std::tie(lhs.i, lhs.s) == std::tie(rhs.i, rhs.s); }
 
   bool
-  operator!=(value_type const& lhs, value_type const& rhs)
-  { return !(lhs == rhs); }
-
-  bool
   operator<(value_type const& lhs, value_type const& rhs)
   { return std::tie(lhs.i, lhs.s) < std::tie(rhs.i, rhs.s); }
 
diff --git a/libstdc++-v3/testsuite/experimental/optional/relops/3.cc b/libstdc++-v3/testsuite/experimental/optional/relops/3.cc
index 9729000..581d016 100644
--- a/libstdc++-v3/testsuite/experimental/optional/relops/3.cc
+++ b/libstdc++-v3/testsuite/experimental/optional/relops/3.cc
@@ -37,10 +37,6 @@  namespace ns
   { return std::tie(lhs.i, lhs.s) == std::tie(rhs.i, rhs.s); }
 
   bool
-  operator!=(value_type const& lhs, value_type const& rhs)
-  { return !(lhs == rhs); }
-
-  bool
   operator<(value_type const& lhs, value_type const& rhs)
   { return std::tie(lhs.i, lhs.s) < std::tie(rhs.i, rhs.s); }
 
diff --git a/libstdc++-v3/testsuite/experimental/optional/relops/4.cc b/libstdc++-v3/testsuite/experimental/optional/relops/4.cc
index 45378f6..ce16fcb 100644
--- a/libstdc++-v3/testsuite/experimental/optional/relops/4.cc
+++ b/libstdc++-v3/testsuite/experimental/optional/relops/4.cc
@@ -37,10 +37,6 @@  namespace ns
   { return std::tie(lhs.i, lhs.s) == std::tie(rhs.i, rhs.s); }
 
   bool
-  operator!=(value_type const& lhs, value_type const& rhs)
-  { return !(lhs == rhs); }
-
-  bool
   operator<(value_type const& lhs, value_type const& rhs)
   { return std::tie(lhs.i, lhs.s) < std::tie(rhs.i, rhs.s); }
 
diff --git a/libstdc++-v3/testsuite/experimental/optional/relops/5.cc b/libstdc++-v3/testsuite/experimental/optional/relops/5.cc
index 008409e..c01bba5 100644
--- a/libstdc++-v3/testsuite/experimental/optional/relops/5.cc
+++ b/libstdc++-v3/testsuite/experimental/optional/relops/5.cc
@@ -37,10 +37,6 @@  namespace ns
   { return std::tie(lhs.i, lhs.s) == std::tie(rhs.i, rhs.s); }
 
   bool
-  operator!=(value_type const& lhs, value_type const& rhs)
-  { return !(lhs == rhs); }
-
-  bool
   operator<(value_type const& lhs, value_type const& rhs)
   { return std::tie(lhs.i, lhs.s) < std::tie(rhs.i, rhs.s); }
 
diff --git a/libstdc++-v3/testsuite/experimental/optional/relops/6.cc b/libstdc++-v3/testsuite/experimental/optional/relops/6.cc
index b179140..a24622b 100644
--- a/libstdc++-v3/testsuite/experimental/optional/relops/6.cc
+++ b/libstdc++-v3/testsuite/experimental/optional/relops/6.cc
@@ -37,10 +37,6 @@  namespace ns
   { return std::tie(lhs.i, lhs.s) == std::tie(rhs.i, rhs.s); }
 
   bool
-  operator!=(value_type const& lhs, value_type const& rhs)
-  { return !(lhs == rhs); }
-
-  bool
   operator<(value_type const& lhs, value_type const& rhs)
   { return std::tie(lhs.i, lhs.s) < std::tie(rhs.i, rhs.s); }