Skip to content

Instantly share code, notes, and snippets.

@mtao
Created October 4, 2017 17:44
Show Gist options
  • Save mtao/8a9e80183a3738f43fd235b50650b4c2 to your computer and use it in GitHub Desktop.
Save mtao/8a9e80183a3738f43fd235b50650b4c2 to your computer and use it in GitHub Desktop.
a small wrapper to enable ranged for loops.
template <typename TokType>
struct range {
public:
range(const TokType& a, const TokType& b): m_begin(a), m_end(b) {}
TokType begin() const { return m_begin;}
TokType end() const { return m_end;}
private:
const TokType& m_begin,m_end;
};
template <typename TokType>
auto make_range(const TokType& begin, const TokType& end) {
return range<TokType>{begin,end};
}
#include <iostream>
int main() {
int data[10] = {0,1,2,3,4,5,6,7,8,9};
for(int v: make_range(data+0,data+10)) {
std::cout << v << ", ";
}
std::cout << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment