[libcxx] r323147 - Still more P0202 constexpr-ifying. This batch is: for_each/for_each_n/lexicographical_compare

Marshall Clow via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 22 12:44:34 PST 2018


Author: marshall
Date: Mon Jan 22 12:44:33 2018
New Revision: 323147

URL: http://llvm.org/viewvc/llvm-project?rev=323147&view=rev
Log:
Still more P0202 constexpr-ifying. This batch is: for_each/for_each_n/lexicographical_compare

Modified:
    libcxx/trunk/include/algorithm
    libcxx/trunk/test/std/algorithms/alg.modifying.operations/alg.partitions/partition_copy.pass.cpp
    libcxx/trunk/test/std/algorithms/alg.nonmodifying/alg.foreach/for_each_n.pass.cpp
    libcxx/trunk/test/std/algorithms/alg.nonmodifying/alg.foreach/test.pass.cpp
    libcxx/trunk/test/std/algorithms/alg.sorting/alg.lex.comparison/lexicographical_compare.pass.cpp
    libcxx/trunk/test/std/algorithms/alg.sorting/alg.lex.comparison/lexicographical_compare_comp.pass.cpp

Modified: libcxx/trunk/include/algorithm
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/algorithm?rev=323147&r1=323146&r2=323147&view=diff
==============================================================================
--- libcxx/trunk/include/algorithm (original)
+++ libcxx/trunk/include/algorithm Mon Jan 22 12:44:33 2018
@@ -32,11 +32,12 @@ template <class InputIterator, class Pre
     none_of(InputIterator first, InputIterator last, Predicate pred);
 
 template <class InputIterator, class Function>
-    Function
+    constexpr Function          // constexpr in C++20
     for_each(InputIterator first, InputIterator last, Function f);
 
 template<class InputIterator, class Size, class Function>
-    InputIterator for_each_n(InputIterator first, Size n, Function f); // C++17
+    constexpr InputIterator     // constexpr in C++20
+    for_each_n(InputIterator first, Size n, Function f); // C++17
 
 template <class InputIterator, class T>
     constexpr InputIterator     // constexpr in C++20
@@ -311,7 +312,7 @@ template <class ForwardIterator, class P
 
 template <class InputIterator, class OutputIterator1,
           class OutputIterator2, class Predicate>
-    pair<OutputIterator1, OutputIterator2>
+    constexpr pair<OutputIterator1, OutputIterator2>   // constexpr in C++20
     partition_copy(InputIterator first, InputIterator last,
                    OutputIterator1 out_true, OutputIterator2 out_false,
                    Predicate pred);
@@ -607,11 +608,11 @@ template<class T, class Compare>
     minmax(initializer_list<T> t, Compare comp);  // constexpr in C++14
 
 template <class InputIterator1, class InputIterator2>
-    bool
+    constexpr bool     // constexpr in C++20
     lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
 
 template <class InputIterator1, class InputIterator2, class Compare>
-    bool
+    constexpr bool     // constexpr in C++20
     lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
                             InputIterator2 first2, InputIterator2 last2, Compare comp);
 
@@ -958,7 +959,7 @@ none_of(_InputIterator __first, _InputIt
 // for_each
 
 template <class _InputIterator, class _Function>
-inline _LIBCPP_INLINE_VISIBILITY
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
 _Function
 for_each(_InputIterator __first, _InputIterator __last, _Function __f)
 {
@@ -971,7 +972,7 @@ for_each(_InputIterator __first, _InputI
 // for_each_n
 
 template <class _InputIterator, class _Size, class _Function>
-inline _LIBCPP_INLINE_VISIBILITY
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
 _InputIterator
 for_each_n(_InputIterator __first, _Size __orig_n, _Function __f)
 {
@@ -3288,7 +3289,7 @@ partition(_ForwardIterator __first, _For
 
 template <class _InputIterator, class _OutputIterator1,
           class _OutputIterator2, class _Predicate>
-pair<_OutputIterator1, _OutputIterator2>
+_LIBCPP_CONSTEXPR_AFTER_CXX17 pair<_OutputIterator1, _OutputIterator2>
 partition_copy(_InputIterator __first, _InputIterator __last,
                _OutputIterator1 __out_true, _OutputIterator2 __out_false,
                _Predicate __pred)
@@ -5658,7 +5659,7 @@ set_symmetric_difference(_InputIterator1
 // lexicographical_compare
 
 template <class _Compare, class _InputIterator1, class _InputIterator2>
-bool
+_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
 __lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
                           _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
 {
@@ -5673,7 +5674,7 @@ __lexicographical_compare(_InputIterator
 }
 
 template <class _InputIterator1, class _InputIterator2, class _Compare>
-inline _LIBCPP_INLINE_VISIBILITY
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
 bool
 lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
                         _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
@@ -5689,7 +5690,7 @@ lexicographical_compare(_InputIterator1
 }
 
 template <class _InputIterator1, class _InputIterator2>
-inline _LIBCPP_INLINE_VISIBILITY
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
 bool
 lexicographical_compare(_InputIterator1 __first1, _InputIterator1 __last1,
                         _InputIterator2 __first2, _InputIterator2 __last2)

Modified: libcxx/trunk/test/std/algorithms/alg.modifying.operations/alg.partitions/partition_copy.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/algorithms/alg.modifying.operations/alg.partitions/partition_copy.pass.cpp?rev=323147&r1=323146&r2=323147&view=diff
==============================================================================
--- libcxx/trunk/test/std/algorithms/alg.modifying.operations/alg.partitions/partition_copy.pass.cpp (original)
+++ libcxx/trunk/test/std/algorithms/alg.modifying.operations/alg.partitions/partition_copy.pass.cpp Mon Jan 22 12:44:33 2018
@@ -11,7 +11,7 @@
 
 // template <class InputIterator, class OutputIterator1,
 //           class OutputIterator2, class Predicate>
-//     pair<OutputIterator1, OutputIterator2>
+//     constexpr pair<OutputIterator1, OutputIterator2>     // constexpr after C++17
 //     partition_copy(InputIterator first, InputIterator last,
 //                    OutputIterator1 out_true, OutputIterator2 out_false,
 //                    Predicate pred);
@@ -19,13 +19,31 @@
 #include <algorithm>
 #include <cassert>
 
+#include "test_macros.h"
 #include "test_iterators.h"
 
 struct is_odd
 {
-    bool operator()(const int& i) const {return i & 1;}
+    TEST_CONSTEXPR bool operator()(const int& i) const {return i & 1;}
 };
 
+#if TEST_STD_VER > 17
+TEST_CONSTEXPR bool test_constexpr() {
+    int ia[] = {1, 3, 5, 2, 4, 6};
+    int r1[10] = {0};
+    int r2[10] = {0};
+    
+    auto p = std::partition_copy(std::begin(ia), std::end(ia), 
+                    std::begin(r1), std::begin(r2), is_odd());
+
+    return std::all_of(std::begin(r1), p.first, is_odd())
+        && std::all_of(p.first, std::end(r1), [](int a){return a == 0;})
+        && std::none_of(std::begin(r2), p.second, is_odd())
+        && std::all_of(p.second, std::end(r2), [](int a){return a == 0;})
+           ;
+    }
+#endif
+
 int main()
 {
     {
@@ -47,4 +65,8 @@ int main()
         assert(r2[2] == 6);
         assert(r2[3] == 8);
     }
+
+#if TEST_STD_VER > 17
+    static_assert(test_constexpr());
+#endif
 }

Modified: libcxx/trunk/test/std/algorithms/alg.nonmodifying/alg.foreach/for_each_n.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/algorithms/alg.nonmodifying/alg.foreach/for_each_n.pass.cpp?rev=323147&r1=323146&r2=323147&view=diff
==============================================================================
--- libcxx/trunk/test/std/algorithms/alg.nonmodifying/alg.foreach/for_each_n.pass.cpp (original)
+++ libcxx/trunk/test/std/algorithms/alg.nonmodifying/alg.foreach/for_each_n.pass.cpp Mon Jan 22 12:44:33 2018
@@ -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
 }

Modified: libcxx/trunk/test/std/algorithms/alg.nonmodifying/alg.foreach/test.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/algorithms/alg.nonmodifying/alg.foreach/test.pass.cpp?rev=323147&r1=323146&r2=323147&view=diff
==============================================================================
--- libcxx/trunk/test/std/algorithms/alg.nonmodifying/alg.foreach/test.pass.cpp (original)
+++ libcxx/trunk/test/std/algorithms/alg.nonmodifying/alg.foreach/test.pass.cpp Mon Jan 22 12:44:33 2018
@@ -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
 }

Modified: libcxx/trunk/test/std/algorithms/alg.sorting/alg.lex.comparison/lexicographical_compare.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/algorithms/alg.sorting/alg.lex.comparison/lexicographical_compare.pass.cpp?rev=323147&r1=323146&r2=323147&view=diff
==============================================================================
--- libcxx/trunk/test/std/algorithms/alg.sorting/alg.lex.comparison/lexicographical_compare.pass.cpp (original)
+++ libcxx/trunk/test/std/algorithms/alg.sorting/alg.lex.comparison/lexicographical_compare.pass.cpp Mon Jan 22 12:44:33 2018
@@ -12,14 +12,26 @@
 // template<InputIterator Iter1, InputIterator Iter2>
 //   requires HasLess<Iter1::value_type, Iter2::value_type>
 //         && HasLess<Iter2::value_type, Iter1::value_type>
-//   bool
+//   constexpr bool             // constexpr after C++17
 //   lexicographical_compare(Iter1 first1, Iter1 last1, Iter2 first2, Iter2 last2);
 
 #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, 2, 3};
+    int ib[] = {1, 3, 5, 2, 4, 6};
+
+    return  std::lexicographical_compare(std::begin(ia), std::end(ia), std::begin(ib), std::end(ib))
+        && !std::lexicographical_compare(std::begin(ib), std::end(ib), std::begin(ia), std::end(ia))
+           ;
+    }
+#endif
+
 template <class Iter1, class Iter2>
 void
 test()
@@ -66,4 +78,8 @@ int main()
     test<const int*, bidirectional_iterator<const int*> >();
     test<const int*, random_access_iterator<const int*> >();
     test<const int*, const int*>();
+
+#if TEST_STD_VER > 17
+    static_assert(test_constexpr());
+#endif
 }

Modified: libcxx/trunk/test/std/algorithms/alg.sorting/alg.lex.comparison/lexicographical_compare_comp.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/algorithms/alg.sorting/alg.lex.comparison/lexicographical_compare_comp.pass.cpp?rev=323147&r1=323146&r2=323147&view=diff
==============================================================================
--- libcxx/trunk/test/std/algorithms/alg.sorting/alg.lex.comparison/lexicographical_compare_comp.pass.cpp (original)
+++ libcxx/trunk/test/std/algorithms/alg.sorting/alg.lex.comparison/lexicographical_compare_comp.pass.cpp Mon Jan 22 12:44:33 2018
@@ -12,7 +12,7 @@
 // template<InputIterator Iter1, InputIterator Iter2, CopyConstructible Compare>
 //   requires Predicate<Compare, Iter1::value_type, Iter2::value_type>
 //         && Predicate<Compare, Iter2::value_type, Iter1::value_type>
-//   bool
+//   constexpr bool             // constexpr after C++17
 //   lexicographical_compare(Iter1 first1, Iter1 last1,
 //                           Iter2 first2, Iter2 last2, Compare comp);
 
@@ -20,8 +20,21 @@
 #include <functional>
 #include <cassert>
 
+#include "test_macros.h"
 #include "test_iterators.h"
 
+#if TEST_STD_VER > 17
+TEST_CONSTEXPR bool test_constexpr() {
+    int ia[] = {1, 2, 3};
+    int ib[] = {1, 3, 5, 2, 4, 6};
+
+    std::greater<int> pred;
+    return !std::lexicographical_compare(std::begin(ia), std::end(ia), std::begin(ib), std::end(ib), pred)
+        &&  std::lexicographical_compare(std::begin(ib), std::end(ib), std::begin(ia), std::end(ia), pred)
+           ;
+    }
+#endif
+
 template <class Iter1, class Iter2>
 void
 test()
@@ -70,4 +83,8 @@ int main()
     test<const int*, bidirectional_iterator<const int*> >();
     test<const int*, random_access_iterator<const int*> >();
     test<const int*, const int*>();
+
+#if TEST_STD_VER > 17
+    static_assert(test_constexpr());
+#endif
 }




More information about the cfe-commits mailing list