What can Rvalue change?

Anyone please give an example of a "volatile value"? My understanding is rvalue appearing on the right side of the "=" expression in the expression. I tested the following example, but I'm not sure if it explains the "mutable rvalue"

int i=1
int &j = i;
j=2;  //cout: i == 2, 

      

+3


source to share


1 answer


It depends to some extent on the context of the phrase "modifiable rvalue". However, this is one possible example:

struct Modifiable
{
  int x;
  void modify() { std::cout << x << '\n'; x = -x; std::cout << x << '\n'; }
};

Modifiable demo()
{
  Modifiable m;
  m.x = 42;
  return m;
}

int main()
{
  demo().modify();
}

      



The return value demo()

is an rvalue (even a prvalue), but changed by the call modify()

.

[Live example]

+5


source







All Articles