Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are tokens wrapped by parentheses not r-value expressions?

Consider the following code:

#include <iostream>

struct Foo
{
  Foo() : bar( 0 ) {}

  int bar;
};


int main()
{
  Foo foo;

  ++(foo.bar);

  std::cout<< foo.bar << std::endl;

  system("pause");
  return 0;
};

Why does foo.bar evaluate to 1?

Doesn't the parentheses in (foo.bar) create an unnamed (r-value) expression which is then incremented?

like image 589
Martin85 Avatar asked Oct 31 '12 08:10

Martin85


1 Answers

Because the standard explicitly states that in 3.4.2 para 6:

A parenthesized expression is a primary expression whose type and value are identical to those of the enclosed expression. The presence of parentheses does not affect whether the expression is an lvalue.

emphasis mine.

like image 69
SingerOfTheFall Avatar answered Oct 17 '22 21:10

SingerOfTheFall