Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why don't the extra parentheses compile? (Can't parenthesize a declaration, maybe?)

16 views
Skip to first unread message

K. Frank

unread,
Feb 18, 2017, 5:29:55 PM2/18/17
to
Hello Group!

Why doesn't the line flagged with the error compile?
(I'm compiling as c++ with g++.)

void f() {
if (false);
if ((false));
bool b;
if (b = false);
if ((b = false));
if (bool b = false);
if ((bool b = false)); // error: expected primary-expression before 'bool'
}

All but the last of the variations compile, but the last
triggers the noted error. What's the language-standard
analysis that explains this?


Thanks for any ideas.


K. Frank

Bo Persson

unread,
Feb 18, 2017, 6:27:08 PM2/18/17
to
It's a grammar thing.

A primary-expression can inclucde a declaration of a variable, but as
soon as you add a '(' it is an '(expression)', which cannot declare
variables.



Bo Persson






K. Frank

unread,
Feb 18, 2017, 6:45:18 PM2/18/17
to
Hi Bo!

On Saturday, February 18, 2017 at 6:27:08 PM UTC-5, Bo Persson wrote:
> On 2017-02-18 23:29, K. Frank wrote:
> > Hello Group!
> >
> > Why doesn't the line flagged with the error compile?
> > ...
> > if ((bool b = false)); // error: expected primary-expression before 'bool'
> ...
> It's a grammar thing.
>
> A primary-expression can inclucde a declaration of a variable, but as
> soon as you add a '(' it is an '(expression)', which cannot declare
> variables.

Thanks. That makes sense (I guess ...)

By the way, I'm speaking from memory here, but is this the
same reason you can use an extra set of parentheses to "fix"
the "most vexing parse" in that the extra set of parentheses
prevents the expression from being parsed as the unwanted
declaration? (I never understood that either.)

> Bo Persson


Best.


K. Frank
0 new messages