libstdc++: Improve diagnostics for misuses of output iterators
authorJonathan Wakely <jwakely@redhat.com>
Wed, 29 Sep 2021 20:19:36 +0000 (21:19 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Tue, 12 Oct 2021 15:05:15 +0000 (16:05 +0100)
This adds deleted overloads so that the errors for invalid uses of
std::advance and std::distance are easier to understand (see for example
PR 102181).

libstdc++-v3/ChangeLog:

* include/bits/stl_iterator_base_funcs.h (__advance): Add
deleted overload to improve diagnostics.
(__distance): Likewise.

libstdc++-v3/include/bits/stl_iterator_base_funcs.h

index e5afab7..fc6e988 100644 (file)
@@ -119,6 +119,13 @@ _GLIBCXX_END_NAMESPACE_CONTAINER
               input_iterator_tag);
 #endif
 
+#if __cplusplus >= 201103L
+  // Give better error if std::distance called with a non-Cpp17InputIterator.
+  template<typename _OutputIterator>
+    void
+    __distance(_OutputIterator, _OutputIterator, output_iterator_tag) = delete;
+#endif
+
   /**
    *  @brief A generalization of pointer arithmetic.
    *  @param  __first  An input iterator.
@@ -186,6 +193,13 @@ _GLIBCXX_END_NAMESPACE_CONTAINER
        __i += __n;
     }
 
+#if __cplusplus >= 201103L
+  // Give better error if std::advance called with a non-Cpp17InputIterator.
+  template<typename _OutputIterator, typename _Distance>
+    void
+    __advance(_OutputIterator&, _Distance, output_iterator_tag) = delete;
+#endif
+
   /**
    *  @brief A generalization of pointer arithmetic.
    *  @param  __i  An input iterator.