The formal definition (in set theory) of a natural number n is as follows:
- 0 is the empty set
- 1 = {0}
- n = {0,1,...,n-1}
I think this would make some C++ code much simpler, if I was allowed to do this:
for (int n : 10)
cout << n << endl;
and it printed numbers from 0 to 9.
So I tried doing the following, which doesn't compile:
#include <iostream>
#include <boost/iterator/counting_iterator.hpp>
boost::counting_iterator<int> begin(int t)
{
return boost::counting_iterator<int>(0);
}
boost::counting_iterator<int> end(int t)
{
return boost::counting_iterator<int>(t);
}
int main()
{
for (int t : 10)
std::cout << t << std::endl;
return 0;
}
Any suggestions on how to achieve this? I get the following error with clang++:
main.cpp:22:20: error: invalid range expression of type 'int'; no viable 'begin' function available
for (int t : 10)
^ ~~
but I think I should be allowed to do this! :)
Edit: I know I can "fake" it if I add the word "range" (or some other word) in the for loop, but I'm wondering if it's possible to do it without.