Make RangeEnumerator C++17 compliant (Generalizing the Range-Based For Loop)
authorChristopher Dykes <cdykes@fb.com>
Tue, 26 Jul 2016 22:48:54 +0000 (15:48 -0700)
committerFacebook Github Bot 5 <facebook-github-bot-5-bot@fb.com>
Tue, 26 Jul 2016 22:53:45 +0000 (15:53 -0700)
Summary:
Specifically the loosening of the definition of a range where-by the end of an iterator may be represented by a different type than the beginning of the range.
Oh, and it also fixes compilation on MSVC, which didn't like the decltype being used to determine the iterator type.

Reviewed By: yfeldblum

Differential Revision: D3613993

fbshipit-source-id: 2940a15d0f93c5b6310d0b1896f5d12ca9aec639

folly/Enumerate.h

index 2a2ce2201f79726b67a6958ac8b6d71602946c53..5733bfb6fa528e542c3849c5840dea3433a363df 100644 (file)
@@ -124,16 +124,17 @@ class Enumerator {
 template <class Range>
 class RangeEnumerator {
   Range r_;
-  using Iterator = decltype(r_.begin());
+  using BeginIteratorType = decltype(std::declval<Range>().begin());
+  using EndIteratorType = decltype(std::declval<Range>().end());
 
  public:
   explicit RangeEnumerator(Range&& r) : r_(std::forward<Range>(r)) {}
 
-  Enumerator<Iterator> begin() {
-    return Enumerator<Iterator>(r_.begin());
+  Enumerator<BeginIteratorType> begin() {
+    return Enumerator<BeginIteratorType>(r_.begin());
   }
-  Enumerator<Iterator> end() {
-    return Enumerator<Iterator>(r_.end());
+  Enumerator<EndIteratorType> end() {
+    return Enumerator<EndIteratorType>(r_.end());
   }
 };