[libcxx-commits] [libcxx] [libc++] Merge the implementations of ranges::copy_n and std::copy_n and fix vector::insert to assign (PR #157444)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Wed Oct 1 08:05:20 PDT 2025


================
@@ -13,48 +13,51 @@
 #include <__config>
 #include <__iterator/iterator_traits.h>
 #include <__type_traits/enable_if.h>
-#include <__utility/convert_to_integral.h>
+#include <__utility/move.h>
+#include <__utility/pair.h>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header
 #endif
 
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
 _LIBCPP_BEGIN_NAMESPACE_STD
 
-template <class _InputIterator,
-          class _Size,
-          class _OutputIterator,
-          __enable_if_t<__has_input_iterator_category<_InputIterator>::value &&
-                            !__has_random_access_iterator_category<_InputIterator>::value,
-                        int> = 0>
-inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _OutputIterator
-copy_n(_InputIterator __first, _Size __orig_n, _OutputIterator __result) {
-  typedef decltype(std::__convert_to_integral(__orig_n)) _IntegralSize;
-  _IntegralSize __n = __orig_n;
-  if (__n > 0) {
----------------
ldionne wrote:

Since it looks like libstdc++ behaves the same as us here, I'm not sure we want to open this can of worms as part of a refactoring: https://godbolt.org/z/59PE89TT5.

Let's add an overload for input iterators (with a suitable comment) just for the classic algo.

CF https://github.com/llvm/llvm-project/commit/99847d2bf132854fffa019bab19818768102ccad

https://github.com/llvm/llvm-project/pull/157444


More information about the libcxx-commits mailing list