Created
February 7, 2016 07:00
-
-
Save 1995hnagamin/7358fe801de5d941f048 to your computer and use it in GitHub Desktop.
ユーザー定義リテラルで整数のRange-based for
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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