Jump to content

  • Log In with Google      Sign In   
  • Create Account

#Actualfrob

Posted 20 March 2013 - 06:01 PM

In simpler terms...

Every expression in c++ is either an lvalue or an rvalue. The names come from which side of the assignment operator they can exist on.

An lvalue is (basically) any named object. All variables are lvalues. An lvalue is something that can exist for more than a single statement.

An rvalue is a temporary value that does not necessarily exist for more than a single statement. The result of a function, as in you example, is an rvalue. The value exists only for a single expression then is lost.

Compilers are very aggressive at optimizing rvalues out of existence; often they only exist in a register or can be sometimes eliminated completely.

You cannot take an address of an rvalue. It is temporary and may not even really exist.

You need to create an lvalue, and that lvalue can be passed by reference to the second function.

Alternatively you can pass it as a const reference. The compiler is allowed to (essentially) create a temporary constant object and pass that object by reference.


Or written differently: because it is passed by non-const reference you must create a variable, assign the first function's result to the variable, and then pass the variable to the second function. Or you will need to modify your function to accept a const reference; do this if you can guarantee you will never modify the input.

#1frob

Posted 20 March 2013 - 05:57 PM

In simpler terms...

Every expression in c++ is either an lvalue or an rvalue. The names come from which side of the assignment operator they can exist on.

An lvalue is (basically) any named object. All variables are lvalues. An lvalue is something that can exist for more than a single statement.

An rvalue is a temporary value that does not necessarily exist for more than a single statement. The result of a function, as in you example, is an rvalue. The value exists only for a single expression then is lost.

Compilers are very aggressive at optimizing rvalues out of existence; often they only exist in a register or can be sometimes eliminated completely.

You cannot take an address of an rvalue. It is temporary and may not even really exist.

You need to create an lvalue, and that lvalue can be passed by reference to the second function.


Or written differently: because it is passed by reference you must create a variable, assign the first function's result to the variable, and then pass the variable to the second function.

PARTNERS