We propose new versions of the if
and switch
statements for C++:
if (init; condition)
and switch
(init; condition)
.
These statements simplify common code patterns and help users keep scopes tight.
switch
and to support if constexpr
following EWG guidance.Before the proposal | With the proposal |
---|---|
{
auto p = m.try_emplace(key, value);
if (!p.second) {
FATAL("Element already registered");
} else {
process(p.second);
}
} |
if (auto p = m.try_emplace(key, value); !p.second) {
FATAL("Element already registered");
} else {
process(p.second);
} |
status_code foo() {
{
status_code c = bar();
if (c != SUCCESS) {
return c;
}
}
// ...
} |
status_code foo() {
if (status_code c = bar(); c != SUCCESS) {
return c;
}
// ...
} |
void safe_init() {
{
std::lock_guard<std::mutex> lk(mx_);
if (v.empty())
v.push_back(kInitialValue);
}
}
// ...
} |
void safe_init() {
if (std::lock_guard<std::mutex> lk(mx_); v.empty()) {
v.push_back(kInitialValue);
}
// ...
}
(Consider having to move this code around.) |
{
Foo gadget(args);
switch (auto s = gadget.status()) {
case OK: gadget.zip(); break;
case Bad: throw BadFoo(s.message());
}
} |
switch (Foo gadget(args); auto s = gadget.status()) {
case OK: gadget.zip(); break;
case Bad: throw BadFoo(s.message());
} |
There are three statements in C++, if
, for
and while
,
which are all variations on a theme. We propose to make the picture more complete by adding
a new form of if
statement.
Statement | Equivalent to* | Iterations |
---|---|---|
while(cond) E; |
{ while(cond) { E; } } |
Repeatedly while cond holds |
for (init; cond; inc) E; |
{ init; while(cond) { E; inc; } } |
Repeatedly while cond holds |
if (cond) E; |
{ while(cond) { E; break; } } |
Once while cond holds |
if (cond) E; else F; |
(more complex)† | Once |
if (init; cond) E; |
{ init; while(cond) { E; break; } } |
Once while cond holds |
if (init; cond) E; else F; |
(more complex)† | Once |
break
and continue
have different semantics in loops.else
blocks in terms of while
is due to the
absence of a fundamental while ... else
construction from the language, which would in some sense be a “universal control structure”.
The switch
statement too uses similar grammar, switch (cond)
,
and we also propose to extend this statement to allow the new form switch (init; cond)
.
This extension was not part of the previous revision of this proposal, but EWG suggested in Oulu
that we may as well offer the same set of syntactic forms for all control structures consistently.
The new form of the if
statement has many uses. Currently, the initializer is
either declared before the statement and leaked into the ambient scope, or an explicit scope
is used. With the new form, such code can be written more compactly, and the improved scope
control makes some erstwhile error-prone constructions a bit more robust:
A certain “monadic” style of bubbling up non-success status values also becomes more compact:
There is always the alternative of using an equivalent construction, namely { init;
if (cond) E; }
. However, this construction is more verbose, and users
often use a “lazy approximation” that omits the extra scope. such as:
This is often just as good, but in certain cases where the lifetime of the object created in the initializer is important, such as when locking a mutex, forgetting the extra scope may easily have hard-to-diagnose adverse effects. Moreover, the explicit additional scope is brittle and may get lost during refactoring.
A common naming convention is that the length of a name of should correspond to the size of its scope; offering a convenient way to declare names in tight scopes makes it easier to follow such a principle without either introducing unwanted nesting levels or using artificially long names.
It is possible to write a library gadget that could contain both initialized values and the result of a boolean expression, but all such attempts have turned up something very unsightly.
An alternative language extension could be of the form with (init) if
(cond) E;
. Constructions like this exist in other languages. While certainly
conceivable, such an extension is more expensive (new keyword, more to teach) and
misses out on the opportunity to make an existing facility more consistent.
It is often said that C++ is already complex enough, and any additional complexity needs
to be carefully justified. We believe that the proposed extension is natural and unsurprising,
and thus adds minimal complexity, and perhaps even removes some of the existing differences
among the various control flow statements. There is nothing about the local initialization
that is specific to loop statements, so having it only on the loop and not on the selection
statement seems arbitrary. Had the initializer form of the if
statement been in
the language from the start, it would not have seemed out of place. (At best one might have
wondered why for
is not also spelled while
, or vice versa.)
For the second point, we would like to consider the advantages of the new form of the
if
statement. Names, lifetimes and scopes are fundamental concepts of C++,
and putting the right names into the right scopes is instrumental to understandability
and maintainability. The proposed extension is mere syntactic sugar, but it is a
convenient tool, readily understood by the reader, that allows the user to keep the
scope of auxiliary variables minimal. Current code either requires additional braces
to keep scopes minimal, which are visually noisy (taking up valuable indentation levels!)
and burdensome to refactor (think of keeping all the lines together), or simply omits
the braces, leaking local variables into larger scopes.
Real, existing code bases contain macros that wrap up common idioms (like map lookup and error status propagation), because users find macros the smaller of the two evils compared to leaking lots of local variables or using excessive braces. The proposal removes a common use case for macros.
Note also that the Go language allows initial statements for both if
and
switch
statements.
This is a core language extension. The newly proposed syntax is ill-formed in the current working draft.
Many thanks to Jens Maurer for invaluable help with the wording.
As part of the wording change, we will rename the grammar production for-init-statement to just init-statement. In section 3.3.3 [basic.block.scope], change paragraph 4 as follows.
Names declared in the for-init-statementinit-statement,
the for-range-declaration, and the condition of if
, while
,
for
, and switch
statements […]
Append the following grammar to clause 6 [stmt.stmt].
In section 6.4 [stmt.select], change the grammar in paragraph 1 as follows.
if (
init-statementopt condition )
statementif (
init-statementopt condition )
statement else
statementswitch (
init-statementopt condition )
statement
Change the first sentence as follows.
See 8.3 for the optional attribute-specifier-seq in a condition. [Note: An init-statement ends with a semicolon. – end note] In Clause 6, the term […]
Insert a new paragraph at the end of subsection 6.4.1 [stmt.if].
An if
statement of the form
is equivalent to
and an if
statement of the form
is equivalent to
except that names declared in the init-statement are in the same declarative region as those declared in the condition.
Insert a new paragraph at the end of subsection 6.4.2 [stmt.switch].
A switch
statement of the form
is equivalent to
except that names declared in the init-statement are in the same declarative region as those declared in the condition.
In section 6.5 [stmt.iter], modify paragraph 1 as follows.
for (
;
expressionopt )
statementfor (
for-range-declaration :
for-range-initializer )
statementSee 8.3 for the optional attribute-specifier-seq in a for-range-declaration.
[Note: A for-init-statementAn init-statement ends
with a semicolon. – end note]
In section 6.5.3 [stmt.for], change paragraph 1 as follows.
The for
statement
is equivalent to
except that names declared in the for-init-statementinit-statement are in the same […]
Change paragraph 3 as follows.
In section 7.1.6.4 [dcl.spec.auto], change paragraph 4 as follows.
In Oulu, EWG also requested that this proposal incorporate the addition of if constexpr
,
which has been approved by EWG and CWG in
P0292r2. That proposal
and the present one are largely orthogonal, and the facilities of if constexpr
work
just as well with the extended if
statement from this proposal. For example, the new
grammar will be (with changes appertaining to P0292r2 shown as boxed):
if
constexpr
opt (
init-statementopt condition )
statementif
constexpr
opt (
init-statementopt condition )
statement else
statementswitch (
init-statementopt condition )
statement
Drafting note. Care should be taken to apply the new constexpr
opt
from P0292r2 to the grammar excerpts in the new paragraph in subsection 6.4.1.
The first sentence of the new wording from P0292r2 needs to be modified to make the reference to the constexpr form of the if statement clearer:
constexpr
if
statement is of the form if constexpr
,