c++: function lvalue or rvalue

 ̄綄美尐妖づ 提交于 2019-12-04 08:39:29

L-Values are locations, R-Values are actual values.

So:

  1. since foo() returns a reference(int&), that makes it an lvalue itself.
  2. Correct. foobar() is an rvalue because foobar() returns int.
  3. We don't care that much if a function is an R-Value or not. What we are getting excited about is R-Value references.

The article you pointed to is interesting and I had not considered forwarding or the use in factories before. The reason I was excited about R-Value references was the move semantics, such as this:

BigClass my_function (const int& val, const OtherClass & valb);

BigClass x;
x = my_function(5, other_class_instance);

In that example, x is destroyed, then the return of my_function is copied into x with a copy constructor. To get around that historically, you would write:

void my_function (BigClass *ret, const int& val, const OtherClass & valb);

BigClass x;
my_function(&x, 5, other_class_instance);

which means that now my_function has side effects, plus it isn't as plain to read. Now, with C++11, we can instead write:

BigClass & my_function (const int& val, const OtherClass & valb);

BigClass x;
x = my_function(5, other_class_instance);

And have it operate as efficiently as the second example.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!