Skip to content

Instantly share code, notes, and snippets.

@1995hnagamin
Created February 7, 2016 07:00
Show Gist options
  • Save 1995hnagamin/7358fe801de5d941f048 to your computer and use it in GitHub Desktop.
Save 1995hnagamin/7358fe801de5d941f048 to your computer and use it in GitHub Desktop.
ユーザー定義リテラルで整数のRange-based for
#include <iostream>
namespace iota {
class Iter {
public:
Iter(): iter(0) {}
Iter(int x): iter(x) {}
int operator*() {
return iter;
}
const Iter& operator++() {
iter++;
return *this;
}
bool operator!=(const Iter &other) {
return iter != other.iter;
}
private:
int iter;
};
class Range {
public:
Range(int x): upper_bound(x) {}
Iter begin() {
return Iter();
}
Iter end() {
return Iter(upper_bound);
}
private:
const int upper_bound;
};
Range operator"" _times(unsigned long long x) {
return Range(x);
}
}
int main() {
using iota::operator"" _times;
for (int i : 10_times) {
std::cout << i << std::endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment