[libcxx] r322566 - More constexpr algorithms from P0202. search/search_n

Marshall Clow via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 16 07:48:27 PST 2018


Author: marshall
Date: Tue Jan 16 07:48:27 2018
New Revision: 322566

URL: http://llvm.org/viewvc/llvm-project?rev=322566&view=rev
Log:
More constexpr algorithms from P0202. search/search_n

Modified:
    libcxx/trunk/include/algorithm
    libcxx/trunk/include/functional
    libcxx/trunk/include/type_traits
    libcxx/trunk/test/std/algorithms/alg.nonmodifying/alg.search/search.pass.cpp
    libcxx/trunk/test/std/algorithms/alg.nonmodifying/alg.search/search_n.pass.cpp
    libcxx/trunk/test/std/algorithms/alg.nonmodifying/alg.search/search_n_pred.pass.cpp
    libcxx/trunk/test/std/algorithms/alg.nonmodifying/alg.search/search_pred.pass.cpp

Modified: libcxx/trunk/include/algorithm
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/algorithm?rev=322566&r1=322565&r2=322566&view=diff
==============================================================================
--- libcxx/trunk/include/algorithm (original)
+++ libcxx/trunk/include/algorithm Tue Jan 16 07:48:27 2018
@@ -148,21 +148,21 @@ template<class ForwardIterator1, class F
                    BinaryPredicate pred);  // **C++14**
 
 template <class ForwardIterator1, class ForwardIterator2>
-    ForwardIterator1
+    constexpr ForwardIterator1      // constexpr in C++20
     search(ForwardIterator1 first1, ForwardIterator1 last1,
            ForwardIterator2 first2, ForwardIterator2 last2);
 
 template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
-    ForwardIterator1
+    constexpr ForwardIterator1      // constexpr in C++20
     search(ForwardIterator1 first1, ForwardIterator1 last1,
            ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
 
 template <class ForwardIterator, class Size, class T>
-    ForwardIterator
+    constexpr ForwardIterator       // constexpr in C++20
     search_n(ForwardIterator first, ForwardIterator last, Size count, const T& value);
 
 template <class ForwardIterator, class Size, class T, class BinaryPredicate>
-    ForwardIterator
+    constexpr ForwardIterator       // constexpr in C++20
     search_n(ForwardIterator first, ForwardIterator last,
              Size count, const T& value, BinaryPredicate pred);
 
@@ -1544,7 +1544,7 @@ is_permutation(_ForwardIterator1 __first
 // __search is in <functional>
 
 template <class _ForwardIterator1, class _ForwardIterator2, class _BinaryPredicate>
-inline _LIBCPP_INLINE_VISIBILITY
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
 _ForwardIterator1
 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
        _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred)
@@ -1557,7 +1557,7 @@ search(_ForwardIterator1 __first1, _Forw
 }
 
 template <class _ForwardIterator1, class _ForwardIterator2>
-inline _LIBCPP_INLINE_VISIBILITY
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
 _ForwardIterator1
 search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
        _ForwardIterator2 __first2, _ForwardIterator2 __last2)
@@ -1570,7 +1570,7 @@ search(_ForwardIterator1 __first1, _Forw
 
 #if _LIBCPP_STD_VER > 14
 template <class _ForwardIterator, class _Searcher>
-_LIBCPP_INLINE_VISIBILITY
+_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
 _ForwardIterator search(_ForwardIterator __f, _ForwardIterator __l, const _Searcher &__s)
 { return __s(__f, __l).first; }
 #endif
@@ -1578,7 +1578,7 @@ _ForwardIterator search(_ForwardIterator
 // search_n
 
 template <class _BinaryPredicate, class _ForwardIterator, class _Size, class _Tp>
-_ForwardIterator
+_LIBCPP_CONSTEXPR_AFTER_CXX17 _ForwardIterator
 __search_n(_ForwardIterator __first, _ForwardIterator __last,
            _Size __count, const _Tp& __value_, _BinaryPredicate __pred, forward_iterator_tag)
 {
@@ -1615,7 +1615,7 @@ __search_n(_ForwardIterator __first, _Fo
 }
 
 template <class _BinaryPredicate, class _RandomAccessIterator, class _Size, class _Tp>
-_RandomAccessIterator
+_LIBCPP_CONSTEXPR_AFTER_CXX17 _RandomAccessIterator
 __search_n(_RandomAccessIterator __first, _RandomAccessIterator __last,
            _Size __count, const _Tp& __value_, _BinaryPredicate __pred, random_access_iterator_tag)
 {
@@ -1655,7 +1655,7 @@ __search_n(_RandomAccessIterator __first
 }
 
 template <class _ForwardIterator, class _Size, class _Tp, class _BinaryPredicate>
-inline _LIBCPP_INLINE_VISIBILITY
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
 _ForwardIterator
 search_n(_ForwardIterator __first, _ForwardIterator __last,
          _Size __count, const _Tp& __value_, _BinaryPredicate __pred)
@@ -1666,7 +1666,7 @@ search_n(_ForwardIterator __first, _Forw
 }
 
 template <class _ForwardIterator, class _Size, class _Tp>
-inline _LIBCPP_INLINE_VISIBILITY
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
 _ForwardIterator
 search_n(_ForwardIterator __first, _ForwardIterator __last, _Size __count, const _Tp& __value_)
 {

Modified: libcxx/trunk/include/functional
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/functional?rev=322566&r1=322565&r2=322566&view=diff
==============================================================================
--- libcxx/trunk/include/functional (original)
+++ libcxx/trunk/include/functional Tue Jan 16 07:48:27 2018
@@ -2412,7 +2412,7 @@ __not_fn_imp<decay_t<_RawFunc>> not_fn(_
 // struct hash<T*> in <memory>
 
 template <class _BinaryPredicate, class _ForwardIterator1, class _ForwardIterator2>
-pair<_ForwardIterator1, _ForwardIterator1>
+pair<_ForwardIterator1, _ForwardIterator1> _LIBCPP_CONSTEXPR_AFTER_CXX11
 __search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
          _ForwardIterator2 __first2, _ForwardIterator2 __last2, _BinaryPredicate __pred,
          forward_iterator_tag, forward_iterator_tag)

Modified: libcxx/trunk/include/type_traits
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/type_traits?rev=322566&r1=322565&r2=322566&view=diff
==============================================================================
--- libcxx/trunk/include/type_traits (original)
+++ libcxx/trunk/include/type_traits Tue Jan 16 07:48:27 2018
@@ -4812,39 +4812,39 @@ struct __sfinae_underlying_type
 template <class _Tp>
 struct __sfinae_underlying_type<_Tp, false> {};
 
-inline _LIBCPP_INLINE_VISIBILITY
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
 int __convert_to_integral(int __val) { return __val; }
 
-inline _LIBCPP_INLINE_VISIBILITY
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
 unsigned __convert_to_integral(unsigned __val) { return __val; }
 
-inline _LIBCPP_INLINE_VISIBILITY
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
 long __convert_to_integral(long __val) { return __val; }
 
-inline _LIBCPP_INLINE_VISIBILITY
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
 unsigned long __convert_to_integral(unsigned long __val) { return __val; }
 
-inline _LIBCPP_INLINE_VISIBILITY
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
 long long __convert_to_integral(long long __val) { return __val; }
 
-inline _LIBCPP_INLINE_VISIBILITY
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
 unsigned long long __convert_to_integral(unsigned long long __val) {return __val; }
 
 template<typename _Fp>
-inline _LIBCPP_INLINE_VISIBILITY
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
 typename enable_if<is_floating_point<_Fp>::value, long long>::type
  __convert_to_integral(_Fp __val) { return __val; }
 
 #ifndef _LIBCPP_HAS_NO_INT128
-inline _LIBCPP_INLINE_VISIBILITY
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
 __int128_t __convert_to_integral(__int128_t __val) { return __val; }
 
-inline _LIBCPP_INLINE_VISIBILITY
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
 __uint128_t __convert_to_integral(__uint128_t __val) { return __val; }
 #endif
 
 template <class _Tp>
-inline _LIBCPP_INLINE_VISIBILITY
+inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
 typename __sfinae_underlying_type<_Tp>::__promoted_type
 __convert_to_integral(_Tp __val) { return __val; }
 

Modified: libcxx/trunk/test/std/algorithms/alg.nonmodifying/alg.search/search.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/algorithms/alg.nonmodifying/alg.search/search.pass.cpp?rev=322566&r1=322565&r2=322566&view=diff
==============================================================================
--- libcxx/trunk/test/std/algorithms/alg.nonmodifying/alg.search/search.pass.cpp (original)
+++ libcxx/trunk/test/std/algorithms/alg.nonmodifying/alg.search/search.pass.cpp Tue Jan 16 07:48:27 2018
@@ -11,7 +11,7 @@
 
 // template<ForwardIterator Iter1, ForwardIterator Iter2>
 //   requires HasEqualTo<Iter1::value_type, Iter2::value_type>
-//   Iter1
+//   constexpr Iter1     // constexpr after C++17
 //   search(Iter1 first1, Iter1 last1, Iter2 first2, Iter2 last2);
 //
 //   template<class ForwardIterator, class Searcher>
@@ -21,8 +21,30 @@
 #include <algorithm>
 #include <cassert>
 
+#include "test_macros.h"
 #include "test_iterators.h"
 
+struct MySearcherC {
+    template <typename Iterator>
+    std::pair<Iterator, Iterator>
+    TEST_CONSTEXPR operator() (Iterator b, Iterator e) const
+    {
+        return std::make_pair(b, e);
+    }
+};
+
+#if TEST_STD_VER > 17
+TEST_CONSTEXPR bool test_constexpr() {
+    int ia[] = {0, 1, 2, 3};
+    int ib[] = {0, 1, 5, 3};
+    int ic[] = {0, 1, 2, 0, 1, 2, 3, 0, 1, 2, 3, 4};
+    return    (std::search(std::begin(ic), std::end(ic), std::begin(ia), std::end(ia)) == ic+3)
+           && (std::search(std::begin(ic), std::end(ic), std::begin(ib), std::end(ib)) == std::end(ic))
+           && (std::search(std::begin(ic), std::end(ic), MySearcherC()) == std::begin(ic))
+           ;
+    }
+#endif
+
 int searcher_called = 0;
 
 struct MySearcher {
@@ -97,4 +119,7 @@ int main()
 }
 #endif
 
+#if TEST_STD_VER > 17
+    static_assert(test_constexpr());
+#endif
 }

Modified: libcxx/trunk/test/std/algorithms/alg.nonmodifying/alg.search/search_n.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/algorithms/alg.nonmodifying/alg.search/search_n.pass.cpp?rev=322566&r1=322565&r2=322566&view=diff
==============================================================================
--- libcxx/trunk/test/std/algorithms/alg.nonmodifying/alg.search/search_n.pass.cpp (original)
+++ libcxx/trunk/test/std/algorithms/alg.nonmodifying/alg.search/search_n.pass.cpp Tue Jan 16 07:48:27 2018
@@ -10,16 +10,27 @@
 // <algorithm>
 
 // template<class ForwardIterator, class Size, class T>
-//   ForwardIterator
+//   constexpr ForwardIterator     // constexpr after C++17
 //   search_n(ForwardIterator first, ForwardIterator last, Size count,
 //            const T& value);
 
 #include <algorithm>
 #include <cassert>
 
+#include "test_macros.h"
 #include "test_iterators.h"
 #include "user_defined_integral.hpp"
 
+#if TEST_STD_VER > 17
+TEST_CONSTEXPR bool test_constexpr() {
+    int ia[] = {0, 0, 1, 1, 2, 2};
+    return    (std::search_n(std::begin(ia), std::end(ia), 1, 0) == ia)
+           && (std::search_n(std::begin(ia), std::end(ia), 2, 1) == ia+2)
+           && (std::search_n(std::begin(ia), std::end(ia), 1, 3) == std::end(ia))
+           ;
+    }
+#endif
+
 template <class Iter>
 void
 test()
@@ -74,4 +85,8 @@ int main()
     test<forward_iterator<const int*> >();
     test<bidirectional_iterator<const int*> >();
     test<random_access_iterator<const int*> >();
+
+#if TEST_STD_VER > 17
+    static_assert(test_constexpr());
+#endif
 }

Modified: libcxx/trunk/test/std/algorithms/alg.nonmodifying/alg.search/search_n_pred.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/algorithms/alg.nonmodifying/alg.search/search_n_pred.pass.cpp?rev=322566&r1=322565&r2=322566&view=diff
==============================================================================
--- libcxx/trunk/test/std/algorithms/alg.nonmodifying/alg.search/search_n_pred.pass.cpp (original)
+++ libcxx/trunk/test/std/algorithms/alg.nonmodifying/alg.search/search_n_pred.pass.cpp Tue Jan 16 07:48:27 2018
@@ -10,16 +10,29 @@
 // <algorithm>
 
 // template<class ForwardIterator, class Size, class T, class BinaryPredicate>
-//   ForwardIterator
+//   constexpr ForwardIterator     // constexpr after C++17
 //   search_n(ForwardIterator first, ForwardIterator last, Size count,
 //            const T& value, BinaryPredicate pred);
 
 #include <algorithm>
 #include <cassert>
 
+#include "test_macros.h"
 #include "test_iterators.h"
 #include "user_defined_integral.hpp"
 
+#if TEST_STD_VER > 17
+TEST_CONSTEXPR bool eq(int a, int b) { return a == b; }
+
+TEST_CONSTEXPR bool test_constexpr() {
+    int ia[] = {0, 0, 1, 1, 2, 2};
+    return    (std::search_n(std::begin(ia), std::end(ia), 1, 0, eq) == ia)
+           && (std::search_n(std::begin(ia), std::end(ia), 2, 1, eq) == ia+2)
+           && (std::search_n(std::begin(ia), std::end(ia), 1, 3, eq) == std::end(ia))
+           ;
+    }
+#endif
+
 struct count_equal
 {
     static unsigned count;
@@ -151,4 +164,8 @@ int main()
     test<forward_iterator<const int*> >();
     test<bidirectional_iterator<const int*> >();
     test<random_access_iterator<const int*> >();
+
+#if TEST_STD_VER > 17
+    static_assert(test_constexpr());
+#endif
 }

Modified: libcxx/trunk/test/std/algorithms/alg.nonmodifying/alg.search/search_pred.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/algorithms/alg.nonmodifying/alg.search/search_pred.pass.cpp?rev=322566&r1=322565&r2=322566&view=diff
==============================================================================
--- libcxx/trunk/test/std/algorithms/alg.nonmodifying/alg.search/search_pred.pass.cpp (original)
+++ libcxx/trunk/test/std/algorithms/alg.nonmodifying/alg.search/search_pred.pass.cpp Tue Jan 16 07:48:27 2018
@@ -11,14 +11,28 @@
 
 // template<ForwardIterator Iter1, ForwardIterator Iter2>
 //   requires HasEqualTo<Iter1::value_type, Iter2::value_type>
-//   Iter1
+//   constexpr Iter1     // constexpr after C++17
 //   search(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 eq(int a, int b) { return a == b; }
+
+TEST_CONSTEXPR bool test_constexpr() {
+    int ia[] = {0, 1, 2, 3};
+    int ib[] = {0, 1, 5, 3};
+    int ic[] = {0, 1, 2, 0, 1, 2, 3, 0, 1, 2, 3, 4};
+    return    (std::search(std::begin(ic), std::end(ic), std::begin(ia), std::end(ia), eq) == ic+3)
+           && (std::search(std::begin(ic), std::end(ic), std::begin(ib), std::end(ib), eq) == std::end(ic))
+           ;
+    }
+#endif
+
 struct count_equal
 {
     static unsigned count;
@@ -108,4 +122,8 @@ int main()
     test<random_access_iterator<const int*>, forward_iterator<const int*> >();
     test<random_access_iterator<const int*>, bidirectional_iterator<const int*> >();
     test<random_access_iterator<const int*>, random_access_iterator<const int*> >();
+
+#if TEST_STD_VER > 17
+    static_assert(test_constexpr());
+#endif
 }




More information about the cfe-commits mailing list