diff mbox

libstdc++/60497 (again) - qualify more calls

Message ID 20140513112016.GV10556@redhat.com
State New
Headers show

Commit Message

Jonathan Wakely May 13, 2014, 11:20 a.m. UTC
My fix for PR 60497 didn't cover the rvalue overloads of std::get.

I also noticed we don't handle comparisons properly if tuple elements
have perverse overloaded comparison operators.

Tested x86_64-linux, committed to trunk.
diff mbox

Patch

commit 0f87ec2cdf390a1234a8562e6b3aba95182a6951
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Tue May 13 11:43:32 2014 +0100

    	PR libstdc++/60497
    	* include/std/tuple (get, __tuple_compare): Qualify more calls to
    	prevent ADL. Cast comparison results to bool.
    	* testsuite/20_util/tuple/60497.cc: Test accessing rvalues.
    	* testsuite/20_util/tuple/comparison_operators/overloaded.cc: New.

diff --git a/libstdc++-v3/include/std/tuple b/libstdc++-v3/include/std/tuple
index 03d87d7..5e8766c 100644
--- a/libstdc++-v3/include/std/tuple
+++ b/libstdc++-v3/include/std/tuple
@@ -775,7 +775,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
                     >::type
     get(tuple<_Elements...>&& __t) noexcept
     { return std::forward<typename tuple_element<__i,
-	tuple<_Elements...>>::type&&>(get<__i>(__t)); }
+	tuple<_Elements...>>::type&&>(std::get<__i>(__t)); }
 
 #if __cplusplus > 201103L
   template<typename _Head, size_t __i, typename... _Tail>
@@ -815,16 +815,16 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
       static constexpr bool 
       __eq(const _Tp& __t, const _Up& __u)
       {
-	return (get<__i>(__t) == get<__i>(__u) &&
-		__tuple_compare<0, __i + 1, __j, _Tp, _Up>::__eq(__t, __u));
+	return bool(std::get<__i>(__t) == std::get<__i>(__u))
+	  && __tuple_compare<0, __i + 1, __j, _Tp, _Up>::__eq(__t, __u);
       }
      
       static constexpr bool 
       __less(const _Tp& __t, const _Up& __u)
       {
-	return ((get<__i>(__t) < get<__i>(__u))
-		|| !(get<__i>(__u) < get<__i>(__t)) &&
-		__tuple_compare<0, __i + 1, __j, _Tp, _Up>::__less(__t, __u));
+	return bool(std::get<__i>(__t) < std::get<__i>(__u))
+	  || !bool(std::get<__i>(__u) < std::get<__i>(__t))
+	  && __tuple_compare<0, __i + 1, __j, _Tp, _Up>::__less(__t, __u);
       }
     };
 
diff --git a/libstdc++-v3/testsuite/20_util/tuple/60497.cc b/libstdc++-v3/testsuite/20_util/tuple/60497.cc
index 76d4223..40d053b 100644
--- a/libstdc++-v3/testsuite/20_util/tuple/60497.cc
+++ b/libstdc++-v3/testsuite/20_util/tuple/60497.cc
@@ -35,3 +35,9 @@  auto a = std::get<0>(t);
 auto b = std::get<0>(ct);
 auto c = std::get<element_type>(t);
 auto d = std::get<element_type>(ct);
+
+// same again for rvalues
+auto e = std::get<0>(std::move(t));
+auto f = std::get<0>(std::move(ct));
+auto g = std::get<element_type>(std::move(t));
+auto h = std::get<element_type>(std::move(ct));
diff --git a/libstdc++-v3/testsuite/20_util/tuple/comparison_operators/overloaded.cc b/libstdc++-v3/testsuite/20_util/tuple/comparison_operators/overloaded.cc
new file mode 100644
index 0000000..0a343ae
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/tuple/comparison_operators/overloaded.cc
@@ -0,0 +1,52 @@ 
+// { dg-options "-std=gnu++11" }
+// { dg-do compile }
+
+// Copyright (C) 2014 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+#include <tuple>
+
+// A type that is contextually convertible to bool but cannot be used with
+// the usual logical operators, and/or/not.
+struct TwistedLogic {
+  bool value;
+
+  explicit operator bool() const noexcept { return value; }
+};
+
+template<typename T>
+bool operator&&(const T&, TwistedLogic) = delete;
+
+template<typename T>
+bool operator&&(TwistedLogic, const T&) = delete;
+
+template<typename T>
+bool operator||(const T&, TwistedLogic) = delete;
+
+template<typename T>
+bool operator||(TwistedLogic, const T&) = delete;
+
+bool operator!(TwistedLogic) noexcept = delete;
+
+struct Compares {};
+
+TwistedLogic operator==(const Compares&, const Compares&) { return {true}; }
+TwistedLogic operator<(const Compares&, const Compares&) { return {false}; }
+
+auto a = std::make_tuple(nullptr, Compares{}, 2, 'U');
+auto b = a == a;
+auto c = a < a;