What Metaware High C/C++ iterator-driven for would look like in the C++ standard

This is a description of Metaware High C/C++'s iterator-driven for statement as it would look in the C++ standard. (Metaware High C/C++ supports iterator-driven for in the C language as well. This page only addresses the C++ standard, however.) For usage and rationale, see the aforelinked Frequently Given Answer.

Wording

6.5.5 iterator-function-driven for

  1. An iterator-function-driven for statement causes a the flow of control to call a for iterator function, which in turn may cause the body of the for statement to be executed.

    [Example:
    void iter ( const std::map<int, std::string> & m ) -> (int, std::string)
    {
    	std::map<int, std::string>::iterator i = m.begin();
    	while (i != m.end()) {
    		yield(i->first, i->second);
    		++i;
    	}
    }
    
    void caller ()
    {
    	std::map<int, std::string> m;
    	
    	for i,s <- iter(m)
    		std::cout << i << " " << s << "\n";
    }
    
    end example.]

6.5.5.1 iterator-function-driven for statements

  1. An iterator-function-driven for statement comprises a function call expression, a non-empty list of identifiers, and a statement body.

    iterator-driven-for-statement:
    for identifier-list <- postfix-expression ( expression-listopt ) statement
    identifier-list:
    identifier
    identifier , identifier-list

  2. The identifiers comprise a set of parameter names, and form part of a set of parameter declarations. These parameters are declared in the same declarative region as the statement body. The function call expression is not within this declarative region, and the names are not in scope for that expression.

    The types for the parameter name declarations are taken from the yield specification of the for iterator function that is called, in corresponding order. The identifier list shall contain the same number of identifiers as there are parameters declared in the yield specification.

  3. The statement body of the for statement is executed whenever the called for iterator function yields a tuple of values.

    Whenever the iterator function yields, the parameters are initialized with the values in the yield call, and the statement body of the for statement is executed.

    [Note: The normal scoping rules apply to the statement body. In particular, names in the lexically enclosing block scopes are visible. It is thus, in effect, an anonymous nested function, whose address and automatic variable context are passed to the iterator function and whose formal parameters are declared in the for statement. end note.]

  4. At the end of the statement body, control returns to the iterator function at the point of yielding, and execution continues from there. When control returns from the iterator function, control passes to the end of the for statement, terminating it.

    Jump statements have additional semantics within an iterator-function-driven for statement.

    Within the statement body:

    • a goto statement (§6.6.4) causes control to return to the iterator function at the point of yielding, to then return from the iterator function as if by a return statement at the point of yielding, and then to transfer to the label specified;

      and

    • a return statement (§6.6.3) causes control to return to the iterator function at the point of yielding, to then return from the iterator function as if by a return statement at the point of yielding, and then control to return from the function that encloses the iterator statement itself.

    Within the statement body, and unless within a nested iteration statement:

    • a continue statement (§6.6.2) causes control to return to the iterator function at the point of yielding and execution to contine from there;

      and

    • a break statement (§6.6.1) causes control to return to the iterator function at the point of yielding and then control to return from the iterator function as-if by a return statement at that point.

6.5.5.2 for iterator functions

  1. A for iterator function is a function returning void that comprises an additional yield specification that is a list of parameter declarations. It is otherwise declared as per §8.3.5.

    for-iterator-function-definition:
    void declarator yield-specification function-body
    yield-specification:
    -> ( parameter-declaration-clause )

    [Note: The yield specification can thus employ default arguments (§ 8.3.6) and the ellipsis, ..., with their same semantics as for ordinary functions (§ 5.2.2). end note.]

    The identifiers in the parameter declarations are not within scope in the body of the function.

  2. A pointer to a for iterator function shall not be used in a function call expression (§5.2.2) except:

    • as the iterator call part of an iterator-function-driven for statement;

      or

    • within the body of an iterator function whose yield specification denotes the same function type (§ 8.3.5).

  3. A for iterator function behaves as if it had an extra, hidden, formal parameter named yield that is a pointer to a function that invokes the body of an iterator-function-driven for statement and whose parameter types are those given in the yield specification. [Note: yield is not a keyword. end note.]

    A (recursive) function call to an iterator function from within an iterator function behaves as if it passes the value of the yield parameter as the appropriate argument to match the hidden formal parameter. [Note: So no variable named yield in an inner block scope will affect what is passed as the hidden argument to the call. end note.]


© Copyright 2011 Jonathan de Boyne Pollard. "Moral" rights asserted.
Permission is hereby granted to copy and to distribute this web page in its original, unmodified form as long as its last modification datestamp information is preserved.