return *this;
}
- bool operator==(const Enumerator& rhs) {
+ template <typename OtherIterator>
+ bool operator==(const Enumerator<OtherIterator>& rhs) {
return it_ == rhs.it_;
}
- bool operator!=(const Enumerator& rhs) {
+ template <typename OtherIterator>
+ bool operator!=(const Enumerator<OtherIterator>& rhs) {
return !(*this == rhs);
}
private:
+ template <typename OtherIterator>
+ friend class Enumerator;
+
Iterator it_;
size_t idx_ = 0;
};
* limitations under the License.
*/
+#include <array>
#include <string>
#include <vector>
EXPECT_TRUE(false);
}
}
+
+class CStringRange {
+ const char* cstr;
+
+ public:
+ struct Sentinel {};
+
+ explicit CStringRange(const char* cstr) : cstr(cstr) {}
+
+ const char* begin() const {
+ return cstr;
+ }
+ Sentinel end() const {
+ return Sentinel{};
+ }
+};
+
+bool operator==(const char* c, CStringRange::Sentinel) {
+ return *c == 0;
+}
+
+TEST(Enumerate, Cpp17Support) {
+ std::array<char, 5> test = {"test"};
+ // Can't use range based for loop until C++17, so test manually
+ // Equivalent to:
+ // for (const auto it : folly::enumerate(CStringRange{test.data()})) { ... }
+ {
+ auto&& enumerate = folly::enumerate(CStringRange{test.data()});
+ auto begin = enumerate.begin();
+ auto end = enumerate.end();
+ for (; begin != end; ++begin) {
+ const auto it = *begin;
+
+ ASSERT_LT(it.index, test.size());
+ EXPECT_EQ(*it, test[it.index]);
+ }
+ }
+}