[gcc r10-9596] libstdc++: Avoid accidental ADL when calling make_reverse_iterator

Jonathan Wakely redi@gcc.gnu.org
Mon Mar 29 20:03:36 GMT 2021


https://gcc.gnu.org/g:a769c39a27685cde2d98ef955da6135383c1182b

commit r10-9596-ga769c39a27685cde2d98ef955da6135383c1182b
Author: Moritz Sichert <sichert@in.tum.de>
Date:   Tue Mar 23 15:47:37 2021 +0000

    libstdc++: Avoid accidental ADL when calling make_reverse_iterator
    
    std::ranges::reverse_view uses make_reverse_iterator in its
    implementation as described in [range.reverse.view]. This accidentally
    allows ADL as an unqualified name is used in the call. According to
    [contents], however, this should be treated as a qualified lookup into
    the std namespace.
    
    This leads to errors due to ambiguous name lookups when another
    make_reverse_iterator function is found via ADL.
    
    libstdc++-v3/Changelog:
    
            * include/std/ranges (reverse_view::begin, reverse_view::end):
            Qualify make_reverse_iterator calls to avoid ADL.
            * testsuite/std/ranges/adaptors/reverse.cc: Test that
            views::reverse works when make_reverse_iterator is defined
            in an associated namespace.
    
    (cherry picked from commit 09f08fef71fb776a1d850a7b854c7ccf8a3d6c11)

Diff:
---
 libstdc++-v3/include/std/ranges                       | 12 ++++++------
 libstdc++-v3/testsuite/std/ranges/adaptors/reverse.cc | 19 +++++++++++++++++++
 2 files changed, 25 insertions(+), 6 deletions(-)

diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges
index a8cc56eb0ec..8e00222c2e8 100644
--- a/libstdc++-v3/include/std/ranges
+++ b/libstdc++-v3/include/std/ranges
@@ -3296,29 +3296,29 @@ namespace views
       {
 	if constexpr (_S_needs_cached_begin)
 	  if (_M_cached_begin._M_has_value())
-	    return make_reverse_iterator(_M_cached_begin._M_get(_M_base));
+	    return std::make_reverse_iterator(_M_cached_begin._M_get(_M_base));
 
 	auto __it = ranges::next(ranges::begin(_M_base), ranges::end(_M_base));
 	if constexpr (_S_needs_cached_begin)
 	  _M_cached_begin._M_set(_M_base, __it);
-	return make_reverse_iterator(std::move(__it));
+	return std::make_reverse_iterator(std::move(__it));
       }
 
       constexpr auto
       begin() requires common_range<_Vp>
-      { return make_reverse_iterator(ranges::end(_M_base)); }
+      { return std::make_reverse_iterator(ranges::end(_M_base)); }
 
       constexpr auto
       begin() const requires common_range<const _Vp>
-      { return make_reverse_iterator(ranges::end(_M_base)); }
+      { return std::make_reverse_iterator(ranges::end(_M_base)); }
 
       constexpr reverse_iterator<iterator_t<_Vp>>
       end()
-      { return make_reverse_iterator(ranges::begin(_M_base)); }
+      { return std::make_reverse_iterator(ranges::begin(_M_base)); }
 
       constexpr auto
       end() const requires common_range<const _Vp>
-      { return make_reverse_iterator(ranges::begin(_M_base)); }
+      { return std::make_reverse_iterator(ranges::begin(_M_base)); }
 
       constexpr auto
       size() requires sized_range<_Vp>
diff --git a/libstdc++-v3/testsuite/std/ranges/adaptors/reverse.cc b/libstdc++-v3/testsuite/std/ranges/adaptors/reverse.cc
index ceaaf3e9e00..37d003d11f0 100644
--- a/libstdc++-v3/testsuite/std/ranges/adaptors/reverse.cc
+++ b/libstdc++-v3/testsuite/std/ranges/adaptors/reverse.cc
@@ -131,6 +131,24 @@ test05()
   VERIFY( test_wrapper<int>::increment_count == 5 );
 }
 
+namespace test_ns
+{
+  struct A {};
+  template <typename T>
+  void make_reverse_iterator(T&&) {}
+} // namespace test_ns
+
+void test06()
+{
+  // Check that views::reverse works and does not use ADL which could lead
+  // to accidentally finding test_ns::make_reverse_iterator(A*).
+  test_ns::A as[] = {{}, {}};
+  auto v = as | std::views::reverse;
+  using V = decltype(v);
+  static_assert( std::ranges::view<V> );
+  static_assert( std::ranges::range<const V> );
+}
+
 int
 main()
 {
@@ -139,4 +157,5 @@ main()
   test03();
   test04();
   test05();
+  test06();
 }


More information about the Gcc-cvs mailing list