[libcxx-commits] [PATCH] D142294: [libc++] Allow transforming to a different type in transform_inclusive_scan
Nikolas Klauser via Phabricator via libcxx-commits
libcxx-commits at lists.llvm.org
Sat Jan 21 18:18:01 PST 2023
philnik created this revision.
philnik added reviewers: ldionne, Mordante, var-const.
Herald added a project: All.
philnik requested review of this revision.
Herald added a project: libc++.
Herald added a subscriber: libcxx-commits.
Herald added a reviewer: libc++.
Fixes #60143
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D142294
Files:
libcxx/include/__numeric/transform_inclusive_scan.h
libcxx/test/std/numerics/numeric.ops/transform.inclusive.scan/transform_inclusive_scan_bop_uop.pass.cpp
Index: libcxx/test/std/numerics/numeric.ops/transform.inclusive.scan/transform_inclusive_scan_bop_uop.pass.cpp
===================================================================
--- libcxx/test/std/numerics/numeric.ops/transform.inclusive.scan/transform_inclusive_scan_bop_uop.pass.cpp
+++ libcxx/test/std/numerics/numeric.ops/transform.inclusive.scan/transform_inclusive_scan_bop_uop.pass.cpp
@@ -36,6 +36,25 @@
}
};
+class MyInt {
+ int i_;
+
+public:
+ constexpr MyInt() = default;
+ constexpr MyInt(int i) : i_(i) {}
+
+ constexpr int get() { return i_; }
+
+ constexpr friend bool operator==(MyInt lhs, MyInt rhs) { return lhs.get() == rhs.get(); }
+};
+
+struct transform_to_type {
+ template <class T>
+ constexpr MyInt operator()(T) const {
+ return {};
+ }
+};
+
template <class Iter1, class BOp, class UOp, class T>
TEST_CONSTEXPR_CXX20 void
test(Iter1 first, Iter1 last, BOp bop, UOp uop, const T *rFirst, const T *rLast)
@@ -112,6 +131,18 @@
std::transform_inclusive_scan(v.begin(), v.end(), res.begin(), std::plus<>(), add_one{});
assert(res.empty());
}
+
+ {
+ std::array a{1, 2, 3, 4};
+ std::array<MyInt, 4> b;
+ std::transform_inclusive_scan(
+ begin(a),
+ end(a),
+ begin(b),
+ [](MyInt lhs, MyInt rhs) { return MyInt{lhs.get() + rhs.get()}; },
+ [](int i) { return MyInt{i + 1}; });
+ assert((b == std::array<MyInt, 4>{2, 5, 9, 14}));
+ }
}
TEST_CONSTEXPR_CXX20 bool
Index: libcxx/include/__numeric/transform_inclusive_scan.h
===================================================================
--- libcxx/include/__numeric/transform_inclusive_scan.h
+++ libcxx/include/__numeric/transform_inclusive_scan.h
@@ -42,7 +42,7 @@
_OutputIterator __result, _BinaryOp __b, _UnaryOp __u)
{
if (__first != __last) {
- typename iterator_traits<_InputIterator>::value_type __init = __u(*__first);
+ auto __init = __u(*__first);
*__result++ = __init;
if (++__first != __last)
return _VSTD::transform_inclusive_scan(__first, __last, __result, __b, __u, __init);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D142294.491113.patch
Type: text/x-patch
Size: 2141 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20230122/671effd5/attachment.bin>
More information about the libcxx-commits
mailing list