[llvm] 0e0e638 - [ADT] Simplify llvm::reverse with constexpr if (NFC)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 20 07:28:21 PDT 2022


Author: Kazu Hirata
Date: 2022-08-20T07:28:03-07:00
New Revision: 0e0e6382493f6eb02453617686a72d784eef421f

URL: https://github.com/llvm/llvm-project/commit/0e0e6382493f6eb02453617686a72d784eef421f
DIFF: https://github.com/llvm/llvm-project/commit/0e0e6382493f6eb02453617686a72d784eef421f.diff

LOG: [ADT] Simplify llvm::reverse with constexpr if (NFC)

Differential Revision: https://reviews.llvm.org/D132279

Added: 
    

Modified: 
    llvm/include/llvm/ADT/STLExtras.h

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h
index 45641ec144248..ae8231a2bc9ca 100644
--- a/llvm/include/llvm/ADT/STLExtras.h
+++ b/llvm/include/llvm/ADT/STLExtras.h
@@ -376,21 +376,12 @@ struct has_rbegin : has_rbegin_impl<typename std::remove_reference<Ty>::type> {
 };
 
 // Returns an iterator_range over the given container which iterates in reverse.
-// Note that the container must have rbegin()/rend() methods for this to work.
-template <typename ContainerTy>
-auto reverse(ContainerTy &&C,
-             std::enable_if_t<has_rbegin<ContainerTy>::value> * = nullptr) {
-  return make_range(C.rbegin(), C.rend());
-}
-
-// Returns an iterator_range over the given container which iterates in reverse.
-// Note that the container must have begin()/end() methods which return
-// bidirectional iterators for this to work.
-template <typename ContainerTy>
-auto reverse(ContainerTy &&C,
-             std::enable_if_t<!has_rbegin<ContainerTy>::value> * = nullptr) {
-  return make_range(std::make_reverse_iterator(std::end(C)),
-                    std::make_reverse_iterator(std::begin(C)));
+template <typename ContainerTy> auto reverse(ContainerTy &&C) {
+  if constexpr (has_rbegin<ContainerTy>::value)
+    return make_range(C.rbegin(), C.rend());
+  else
+    return make_range(std::make_reverse_iterator(std::end(C)),
+                      std::make_reverse_iterator(std::begin(C)));
 }
 
 /// An iterator adaptor that filters the elements of given inner iterators.


        


More information about the llvm-commits mailing list