I'm interested in using the iterator syntax for other types of iteration; specifically not that over a container, but rather just to clean up code that functions like iteration.
I already have something working (and maybe - if it isn't broken, don't fix?), but I'd like to know the formal requirements for an iterator for all cases (i.e. the different types of iterators such as constant, and their categories/tags, such as forward).
Here's what how I understand the implementation from cppreference
{
auto && __range = /* range_expression */;
auto __begin = __range.begin( );
auto __end = __range.end( );
for ( ;
__begin != __end;
++__begin )
{
/* range_declaration */ = *__begin;
/* loop_statement */
}
}
From what I've gathered, the current requirements are:
__range
must contain a non-static rvalue-accessible function member calledbegin
andend
that take no parameters- The return from
begin
must support the pre-increment operator - The return from
begin
must support the dereference operator - The returns from
begin
andend
must support the does not equal operator
But then I look at the standard and see things like const_iterator
and all of the type alias members of the iterator returned from begin
and end
yet I never see them used (except for something like algorithmic functions, in which case the formal requirements aren't there except for the end-coder, and are not actual requirements for someone making their own iterator).