This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[patch] Detect non-member operator&() overloads in std::experimental::optional


The _Has_addressof trait only detected an overloaded operator&() as a
member function, this also checks for a non-member.

Tested x86_64-linux, committed to trunk.
commit 32b294e3eddf7789c25f3b751ae6b2a2c5c43275
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Mon Nov 10 19:02:02 2014 +0000

    	* include/experimental/optional (_Has_addressof): Check for non-member
    	operator&.
    	* testsuite/experimental/optional/observers/2.cc: Add operator&.
    	* testsuite/experimental/optional/constexpr/observers/2.cc: Likewise.

diff --git a/libstdc++-v3/include/experimental/optional b/libstdc++-v3/include/experimental/optional
index 7e01abe..2d3127a 100644
--- a/libstdc++-v3/include/experimental/optional
+++ b/libstdc++-v3/include/experimental/optional
@@ -127,13 +127,23 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   __throw_bad_optional_access(const char* __s)
   { _GLIBCXX_THROW_OR_ABORT(bad_optional_access(__s)); }
 
-  template<typename _Tp, typename _Sfinae = void>
-    struct _Has_addressof_impl : std::false_type { };
+  template<typename _Tp, typename = void>
+    struct _Has_addressof_mem : std::false_type { };
 
   template<typename _Tp>
-    struct _Has_addressof_impl<_Tp,
-      decltype( std::declval<const _Tp&>().operator&(), void() )>
-      : std::true_type { };
+    struct _Has_addressof_mem<_Tp,
+         __void_t<decltype( std::declval<const _Tp&>().operator&() )>
+      >
+    : std::true_type { };
+
+  template<typename _Tp, typename = void>
+    struct _Has_addressof_free : std::false_type { };
+
+  template<typename _Tp>
+    struct _Has_addressof_free<_Tp,
+         __void_t<decltype( operator&(std::declval<const _Tp&>()) )>
+      >
+    : std::true_type { };
 
   /**
     * @brief Trait that detects the presence of an overloaded unary operator&.
@@ -143,7 +153,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     * declval<_Tp * const&>().operator&()).
     */
   template<typename _Tp>
-    struct _Has_addressof : _Has_addressof_impl<_Tp>::type { };
+    struct _Has_addressof
+    : std::__or_<_Has_addressof_mem<_Tp>, _Has_addressof_free<_Tp>>::type
+    { };
 
   /**
     * @brief An overload that attempts to take the address of an lvalue as a
diff --git a/libstdc++-v3/testsuite/experimental/optional/constexpr/observers/2.cc b/libstdc++-v3/testsuite/experimental/optional/constexpr/observers/2.cc
index 14b5e3b..2c9215c 100644
--- a/libstdc++-v3/testsuite/experimental/optional/constexpr/observers/2.cc
+++ b/libstdc++-v3/testsuite/experimental/optional/constexpr/observers/2.cc
@@ -26,6 +26,8 @@
 struct value_type
 {
   int i;
+
+  void* operator&() { return nullptr; } // N.B. non-const
 };
 
 int main()
diff --git a/libstdc++-v3/testsuite/experimental/optional/observers/2.cc b/libstdc++-v3/testsuite/experimental/optional/observers/2.cc
index 8499b47..9fb2edb 100644
--- a/libstdc++-v3/testsuite/experimental/optional/observers/2.cc
+++ b/libstdc++-v3/testsuite/experimental/optional/observers/2.cc
@@ -26,6 +26,8 @@ struct value_type
   int i;
 };
 
+void* operator&(const value_type&) = delete;
+
 int main()
 {
   std::experimental::optional<value_type> o { value_type { 51 } };

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]