Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In current C++ when body of if statements contain only one command then: Parenth

ID: 659063 • Letter: I

Question

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.

Explanation / Answer

I don't believe any such extension could be made. The problem is that there doesn't seem to be any way to tell if a brace is the start of an initializer or not. For example, the following code would appear to be ambiguous under your proposed change:

The two interpretations are:

Create a new T with an empty initializer list, and if the result is not null call "hello".

Create a new T with the default constructor, and if the result is not null, do nothing. Then call "hello" in either case.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote