Still more P0202 constexpr-ifying. This batch is: for_each/for_each_n/lexicographical_compare

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@323147 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Marshall Clow
2018-01-22 20:44:33 +00:00
parent 65d4ee3df0
commit a15161a030
6 changed files with 108 additions and 17 deletions

View File

@@ -11,14 +11,29 @@
// UNSUPPORTED: c++98, c++03, c++11, c++14
// template<class InputIterator, class Size, class Function>
// InputIterator for_each_n(InputIterator first, Size n, Function f);
// constexpr InputIterator // constexpr after C++17
// for_each_n(InputIterator first, Size n, Function f);
#include <algorithm>
#include <cassert>
#include "test_macros.h"
#include "test_iterators.h"
#if TEST_STD_VER > 17
TEST_CONSTEXPR bool test_constexpr() {
int ia[] = {1, 3, 6, 7};
int expected[] = {3, 5, 8, 9};
const size_t N = 4;
auto it = std::for_each_n(std::begin(ia), N, [](int &a) { a += 2; });
return it == (std::begin(ia) + N)
&& std::equal(std::begin(ia), std::end(ia), std::begin(expected))
;
}
#endif
struct for_each_test
{
for_each_test(int c) : count(c) {}
@@ -58,4 +73,8 @@ int main()
for (unsigned i = 0; i < 1; ++i)
assert(ia[i] == static_cast<int>(i+2));
}
#if TEST_STD_VER > 17
static_assert(test_constexpr());
#endif
}

View File

@@ -11,14 +11,26 @@
// template<InputIterator Iter, Callable<auto, Iter::reference> Function>
// requires CopyConstructible<Function>
// Function
// constexpr Function // constexpr after C++17
// for_each(Iter first, Iter last, Function f);
#include <algorithm>
#include <cassert>
#include "test_macros.h"
#include "test_iterators.h"
#if TEST_STD_VER > 17
TEST_CONSTEXPR bool test_constexpr() {
int ia[] = {1, 3, 6, 7};
int expected[] = {3, 5, 8, 9};
std::for_each(std::begin(ia), std::end(ia), [](int &a) { a += 2; });
return std::equal(std::begin(ia), std::end(ia), std::begin(expected))
;
}
#endif
struct for_each_test
{
for_each_test(int c) : count(c) {}
@@ -36,4 +48,8 @@ int main()
assert(f.count == s);
for (unsigned i = 0; i < s; ++i)
assert(ia[i] == static_cast<int>(i+1));
#if TEST_STD_VER > 17
static_assert(test_constexpr());
#endif
}