In current C++ when body of if
statements contain only one command then:
Parenthesis around if
condition are mandatory but block are optional. So, both examples are OK:
if ( condition ) { return 0; }
if ( condition ) return 0;
But is it theoretically possible to do it also oppositely?:
Blocks mandatory and parenthesis around if
condition optional:
if ( condition ) { return 0; }
if condition { return 0; }
Is it teoretically possible to extend C++ syntax this way? (for example as extension in some C++ compiler or theoretically in some future C++ standard). Or collides this hypothetical extension with some other C++ syntactic rule?
Note: Personal opinions if this extension should be made or not are irelevant - that's not the question.
EDIT:
Strict interpretation of question has ben answered by Jules.
But if "optional parenthesis" will not mean "optional in every case" but instead "optional in most of cases", then proposed change in C++ still can be made. In rare corner cases like that in Jules answer, compiler can detect ambiquity, and output error:
"Ambiguous if condition. You must explicitly use parenthesis to resolve it"
condition
, you could easily change the language definition so the parentheses are optional. But C++ syntax is pretty hairy, and I'm not certain that a complexcondition
might not legitimately contain a{
.