[libcxx-commits] [PATCH] D70873: [LIBC++]std::reduce(begin, end, init) does not accept init with only move constructor
kamlesh kumar via Phabricator via libcxx-commits
libcxx-commits at lists.llvm.org
Sat Nov 30 20:08:10 PST 2019
kamleshbhalui created this revision.
kamleshbhalui added reviewers: mclow.lists, ldionne.
kamleshbhalui added a project: libc++.
Herald added subscribers: dexonsmith, christof.
As per the c++ when the user-declare move-constructor compiler deletes the copy constructor.
so modified the std::reduce(begin, end, init) so it does not require copy constructor and behavior matches with
std::reduce(begin, end, init,op).
It fixes this https://bugs.llvm.org/show_bug.cgi?id=43013
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D70873
Files:
libcxx/include/numeric
libcxx/test/std/numerics/numeric.ops/reduce/reduce_init.pass.cpp
Index: libcxx/test/std/numerics/numeric.ops/reduce/reduce_init.pass.cpp
===================================================================
--- libcxx/test/std/numerics/numeric.ops/reduce/reduce_init.pass.cpp
+++ libcxx/test/std/numerics/numeric.ops/reduce/reduce_init.pass.cpp
@@ -65,5 +65,16 @@
test<random_access_iterator<const int*> >();
test<const int*>();
+ //test for checking std::reduce should not require copy constructor for initial object
+ {
+ struct T {
+ T(int){}
+ T(T &&){}
+ T &operator=(T &&){}
+ T operator+(const T &) const{}
+ };
+ T t[1]{1};
+ std::reduce(t, t + 1, T(0));
+ }
return 0;
}
Index: libcxx/include/numeric
===================================================================
--- libcxx/include/numeric
+++ libcxx/include/numeric
@@ -193,7 +193,9 @@
_Tp
reduce(_InputIterator __first, _InputIterator __last, _Tp __init)
{
- return _VSTD::reduce(__first, __last, __init, _VSTD::plus<>());
+ for (; __first != __last; ++__first)
+ __init = _VSTD::plus<>()(__init, *__first);
+ return __init;
}
template <class _InputIterator>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D70873.231607.patch
Type: text/x-patch
Size: 1134 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20191201/3a5caba7/attachment.bin>
More information about the libcxx-commits
mailing list