diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm index 4672810a21a2..9ec146a6798e 100644 --- a/libcxx/include/algorithm +++ b/libcxx/include/algorithm @@ -193,11 +193,11 @@ template iter_swap(ForwardIterator1 a, ForwardIterator2 b); template - OutputIterator + constexpr OutputIterator // constexpr in C++20 transform(InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op); template - OutputIterator + constexpr OutputIterator // constexpr in C++20 transform(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, OutputIterator result, BinaryOperation binary_op); @@ -1946,7 +1946,7 @@ move_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last, // transform template -inline _LIBCPP_INLINE_VISIBILITY +inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator transform(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _UnaryOperation __op) { @@ -1956,7 +1956,7 @@ transform(_InputIterator __first, _InputIterator __last, _OutputIterator __resul } template -inline _LIBCPP_INLINE_VISIBILITY +inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17 _OutputIterator transform(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _OutputIterator __result, _BinaryOperation __binary_op) diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.transform/binary_transform.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.transform/binary_transform.pass.cpp index 8491ea59eff6..27d3a968a768 100644 --- a/libcxx/test/std/algorithms/alg.modifying.operations/alg.transform/binary_transform.pass.cpp +++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.transform/binary_transform.pass.cpp @@ -12,15 +12,34 @@ // template BinaryOp> // requires OutputIterator && CopyConstructible -// OutIter +// constexpr OutIter // constexpr after C++17 // transform(InIter1 first1, InIter1 last1, InIter2 first2, OutIter result, BinaryOp binary_op); #include #include #include +#include "test_macros.h" #include "test_iterators.h" +#if TEST_STD_VER > 17 +TEST_CONSTEXPR bool test_constexpr() { + const int ia[] = {1, 3, 6, 7}; + const int ib[] = {2, 4, 7, 8}; + int ic[] = {0, 0, 0, 0, 0}; // one bigger + const int expected[] = {3, 7, 13, 15}; + + auto it = std::transform(std::begin(ia), std::end(ia), + std::begin(ib), std::begin(ic), std::plus()); + + return it == (std::begin(ic) + std::size(ia)) + && *it == 0 // don't overwrite the last value in the output array + && std::equal(std::begin(expected), std::end(expected), std::begin(ic), it) + ; + } +#endif + + template void test() @@ -214,4 +233,8 @@ int main() test >(); test >(); test(); + +#if TEST_STD_VER > 17 + static_assert(test_constexpr()); +#endif } diff --git a/libcxx/test/std/algorithms/alg.modifying.operations/alg.transform/unary_transform.pass.cpp b/libcxx/test/std/algorithms/alg.modifying.operations/alg.transform/unary_transform.pass.cpp index 6c5e621e4b20..b6f9cbad00fe 100644 --- a/libcxx/test/std/algorithms/alg.modifying.operations/alg.transform/unary_transform.pass.cpp +++ b/libcxx/test/std/algorithms/alg.modifying.operations/alg.transform/unary_transform.pass.cpp @@ -12,16 +12,34 @@ // template Op> // requires OutputIterator && CopyConstructible -// OutIter +// constexpr OutIter // constexpr after C++17 // transform(InIter first, InIter last, OutIter result, Op op); #include #include #include +#include "test_macros.h" #include "test_iterators.h" -int plusOne(int v) { return v + 1; } +TEST_CONSTEXPR int plusOne(int v) { return v + 1; } + + +#if TEST_STD_VER > 17 +TEST_CONSTEXPR bool test_constexpr() { + int ia[] = {1, 3, 6, 7}; + int ib[] = {0, 0, 0, 0, 0}; // one bigger + const int expected[] = {2, 4, 7, 8}; + + auto it = std::transform(std::begin(ia), std::end(ia), std::begin(ib), plusOne); + + return it == (std::begin(ib) + std::size(ia)) + && *it == 0 // don't overwrite the last value in the output array + && std::equal(std::begin(ib), it, std::begin(expected), std::end(expected)) + ; + } +#endif + template void @@ -76,4 +94,8 @@ int main() test >(); test >(); test(); + +#if TEST_STD_VER > 17 + static_assert(test_constexpr()); +#endif }