mirror of
https://github.com/llvm-mirror/libcxx.git
synced 2025-10-24 12:02:36 +08:00
More P0202 constexpr-ifying in <algorithm>. This commit handles 'transform'.
git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@322970 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -193,11 +193,11 @@ template <class ForwardIterator1, class ForwardIterator2>
|
|||||||
iter_swap(ForwardIterator1 a, ForwardIterator2 b);
|
iter_swap(ForwardIterator1 a, ForwardIterator2 b);
|
||||||
|
|
||||||
template <class InputIterator, class OutputIterator, class UnaryOperation>
|
template <class InputIterator, class OutputIterator, class UnaryOperation>
|
||||||
OutputIterator
|
constexpr OutputIterator // constexpr in C++20
|
||||||
transform(InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op);
|
transform(InputIterator first, InputIterator last, OutputIterator result, UnaryOperation op);
|
||||||
|
|
||||||
template <class InputIterator1, class InputIterator2, class OutputIterator, class BinaryOperation>
|
template <class InputIterator1, class InputIterator2, class OutputIterator, class BinaryOperation>
|
||||||
OutputIterator
|
constexpr OutputIterator // constexpr in C++20
|
||||||
transform(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
|
transform(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
|
||||||
OutputIterator result, BinaryOperation binary_op);
|
OutputIterator result, BinaryOperation binary_op);
|
||||||
|
|
||||||
@@ -1946,7 +1946,7 @@ move_backward(_BidirectionalIterator1 __first, _BidirectionalIterator1 __last,
|
|||||||
// transform
|
// transform
|
||||||
|
|
||||||
template <class _InputIterator, class _OutputIterator, class _UnaryOperation>
|
template <class _InputIterator, class _OutputIterator, class _UnaryOperation>
|
||||||
inline _LIBCPP_INLINE_VISIBILITY
|
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
|
||||||
_OutputIterator
|
_OutputIterator
|
||||||
transform(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _UnaryOperation __op)
|
transform(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _UnaryOperation __op)
|
||||||
{
|
{
|
||||||
@@ -1956,7 +1956,7 @@ transform(_InputIterator __first, _InputIterator __last, _OutputIterator __resul
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _BinaryOperation>
|
template <class _InputIterator1, class _InputIterator2, class _OutputIterator, class _BinaryOperation>
|
||||||
inline _LIBCPP_INLINE_VISIBILITY
|
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
|
||||||
_OutputIterator
|
_OutputIterator
|
||||||
transform(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
|
transform(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2,
|
||||||
_OutputIterator __result, _BinaryOperation __binary_op)
|
_OutputIterator __result, _BinaryOperation __binary_op)
|
||||||
|
|||||||
@@ -12,15 +12,34 @@
|
|||||||
// template<InputIterator InIter1, InputIterator InIter2, class OutIter,
|
// template<InputIterator InIter1, InputIterator InIter2, class OutIter,
|
||||||
// Callable<auto, const InIter1::value_type&, const InIter2::value_type&> BinaryOp>
|
// Callable<auto, const InIter1::value_type&, const InIter2::value_type&> BinaryOp>
|
||||||
// requires OutputIterator<OutIter, BinaryOp::result_type> && CopyConstructible<BinaryOp>
|
// requires OutputIterator<OutIter, BinaryOp::result_type> && CopyConstructible<BinaryOp>
|
||||||
// OutIter
|
// constexpr OutIter // constexpr after C++17
|
||||||
// transform(InIter1 first1, InIter1 last1, InIter2 first2, OutIter result, BinaryOp binary_op);
|
// transform(InIter1 first1, InIter1 last1, InIter2 first2, OutIter result, BinaryOp binary_op);
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
|
||||||
|
#include "test_macros.h"
|
||||||
#include "test_iterators.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<int>());
|
||||||
|
|
||||||
|
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<class InIter1, class InIter2, class OutIter>
|
template<class InIter1, class InIter2, class OutIter>
|
||||||
void
|
void
|
||||||
test()
|
test()
|
||||||
@@ -214,4 +233,8 @@ int main()
|
|||||||
test<const int*, const int*, bidirectional_iterator<int*> >();
|
test<const int*, const int*, bidirectional_iterator<int*> >();
|
||||||
test<const int*, const int*, random_access_iterator<int*> >();
|
test<const int*, const int*, random_access_iterator<int*> >();
|
||||||
test<const int*, const int*, int*>();
|
test<const int*, const int*, int*>();
|
||||||
|
|
||||||
|
#if TEST_STD_VER > 17
|
||||||
|
static_assert(test_constexpr());
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,16 +12,34 @@
|
|||||||
// template<InputIterator InIter, class OutIter,
|
// template<InputIterator InIter, class OutIter,
|
||||||
// Callable<auto, const InIter::value_type&> Op>
|
// Callable<auto, const InIter::value_type&> Op>
|
||||||
// requires OutputIterator<OutIter, Op::result_type> && CopyConstructible<Op>
|
// requires OutputIterator<OutIter, Op::result_type> && CopyConstructible<Op>
|
||||||
// OutIter
|
// constexpr OutIter // constexpr after C++17
|
||||||
// transform(InIter first, InIter last, OutIter result, Op op);
|
// transform(InIter first, InIter last, OutIter result, Op op);
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
|
||||||
|
#include "test_macros.h"
|
||||||
#include "test_iterators.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 <class InIter, class OutIter>
|
template <class InIter, class OutIter>
|
||||||
void
|
void
|
||||||
@@ -76,4 +94,8 @@ int main()
|
|||||||
test<const int*, bidirectional_iterator<int*> >();
|
test<const int*, bidirectional_iterator<int*> >();
|
||||||
test<const int*, random_access_iterator<int*> >();
|
test<const int*, random_access_iterator<int*> >();
|
||||||
test<const int*, int*>();
|
test<const int*, int*>();
|
||||||
|
|
||||||
|
#if TEST_STD_VER > 17
|
||||||
|
static_assert(test_constexpr());
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user