When input_iterator<_Out> isn't satisfied, we need to avoid substituting
into iter_value_t<_Out> because the latter isn't necessarily
well-formed in that case.  To that end, this patch rewrites the
problematic condition in ranges::unique_copy into a nested requirement
which has the correct short-circuiting behavior.

Tested on x86_64-pc-linux-gnu, does this look OK?

libstdc++-v3/ChangeLog:

        * include/bits/ranges_algo.h (__unique_copy_fn::operator()):
        Rewrite the constexpr if conjunction into a nested requirement
        for sake of short-circuiting.
        * testsuite/25_algorithms/unique_copy/100770.cc: New test.
---
 libstdc++-v3/include/bits/ranges_algo.h       |  5 +--
 .../25_algorithms/unique_copy/100770.cc       | 33 +++++++++++++++++++
 2 files changed, 36 insertions(+), 2 deletions(-)
 create mode 100644 libstdc++-v3/testsuite/25_algorithms/unique_copy/100770.cc

diff --git a/libstdc++-v3/include/bits/ranges_algo.h 
b/libstdc++-v3/include/bits/ranges_algo.h
index cda3042c11f..ce4719d5642 100644
--- a/libstdc++-v3/include/bits/ranges_algo.h
+++ b/libstdc++-v3/include/bits/ranges_algo.h
@@ -1432,8 +1432,9 @@ namespace ranges
                }
            return {__next, std::move(++__result)};
          }
-       else if constexpr (input_iterator<_Out>
-                          && same_as<iter_value_t<_Iter>, iter_value_t<_Out>>)
+       else if constexpr (requires { requires (input_iterator<_Out>
+                                               && same_as<iter_value_t<_Iter>,
+                                                          
iter_value_t<_Out>>); })
          {
            *__result = *__first;
            while (++__first != __last)
diff --git a/libstdc++-v3/testsuite/25_algorithms/unique_copy/100770.cc 
b/libstdc++-v3/testsuite/25_algorithms/unique_copy/100770.cc
new file mode 100644
index 00000000000..ea49eb46a18
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/unique_copy/100770.cc
@@ -0,0 +1,33 @@
+// Copyright (C) 2021 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/>.
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do compile { target c++2a } }
+
+// PR libstdc++/100770
+
+#include <algorithm>
+#include <sstream>
+
+int main() {
+  std::istringstream str("42 42 42");
+  extern std::ostream out;
+  std::ranges::unique_copy(
+    std::istream_iterator<int>(str),
+    std::istream_iterator<int>(),
+    std::ostream_iterator<int>(out, " "));
+}
-- 
2.32.0.rc0

Reply via email to