11

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?

Martin85
  • 308
  • 4
  • 13

2 Answers2

9

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.

SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105
  • 2
    I was confused, because: `struct Widget{} w;` -> `decltype(w)` and `decltype((w))` are not the same. See also [link](https://skydrive.live.com/view.aspx?resid=F1B8FF18A2AEC5C5!1062), slide 33. – Martin85 Oct 31 '12 at 08:52
  • @user1787909: `decltype` is special. – CB Bailey Oct 31 '12 at 09:19
1

No, parenthesis do not have any meaning besides changing the order of operations.

To create an rvalue you need to use special function std::move(x).

Kirill Kobelev
  • 10,252
  • 6
  • 30
  • 51