Document number: P2608R0

Date: 2022-06-15

Reply-to: Justin Cooke <jgc@cems.de>
Target audience: EWG

 

Allow multiple init-statements

 

Proposal: Wherever the language allows or requires an init-statement, to allow multiple such statements.

 

Motivation: An init-statement is required in for statements and allowed in range-based-for, if and switch statements. An init-statement is commonly used to declare, and optionally initialize, one or more variables. However, because at most one such statement is currently allowed, two or more variables can only be declared if they happen to be of the same type.  This restriction seems to be an accident of syntax rather than a design intention. No syntactic ambiguity would arise from allowing multiple init-statements wherever the language currently allows or requires one init-statement.

 

Example of use:

for (int i = 0, j = 0; i < m; i++)            //OK in C++20

for (int k = 0; double s = 0.; k < n; k++)    //error in C++20, allowed under this proposal

 

Effect on existing code: None. Code using the proposed feature is ill-formed under C++20 and earlier standards.

 

Wording: (edits to N4910)

 

8 Statements                                                     [stmt.stmt]

8.1. Preamble                                                   [stmt.pre]

1

init-statement-seq:

init-statement-seqopt  init-statement

 

2 A substatement of a statement is one of the following:

(2.1) — for a labeled-statement, its statement,

(2.2) — for a compound-statement, any statement of its statement-seq,

(2.3) — for a selection-statement, any of its statements (but not an init-statement in its init-statement-seq), or

(2.4) — for an iteration-statement, its statement (but not an init-statement in its init-statement-seq).

3 A statement S1 encloses a statement S2 if

(3.1) — S2 is a substatement of S1,

(3.2) — S1 is a selection-statement or iteration-statement and S2 is anthe init-statement in the init-statement-seq of S1,

(3.3) — S1 is a try-block and S2 is its compound-statement or any of the compound-statements of its handlers, or

(3.4) — S1 encloses a statement S3 and S3 encloses S2.

 

 

8.5. Selection statements                                 [stmt.select]

{ Replace all occurrences of init-statement with init-statement-seq, except in Note 1 of 8.5.1. }

 

8.6. Iteration statements                                 [stmt.iter]

{ Replace all occurrences of init-statement with init-statement-seq, except in Note 1 of 8.6.1. }

 

A.6 Statements                                                 [gram.stmt]

init-statement-seq:

init-statement-seqopt  init-statement

 

 

Explanation: The proposed syntax will have the effect that:

(i)               all code within the main parentheses of a for statement, up to and including the penultimate semicolon, consists of init-statements;

(ii)              all code within the main parentheses of an if, switch or range-based-for statement, up to and including the last semicolon, if any, consists of init-statements.