Value Categories and References
Rvalue Bindings
An rvalue reference can bind to a temporary expression so the program can name that temporary result.
rvalue reference
An rvalue reference uses `&&` and can bind to a temporary value.
temporary expression
Expressions such as arithmetic results do not have stable names until they are bound or stored.
Rvalue Bindings
rvalue_bindings.cpp
Replay: real traced execution (multi-file project)
#include <iostream>
int main() {
int base = 6;
int&& temporary = base * 2;
int total = temporary + 1;
std::cout << "temporary=" << temporary << std::endl;
std::cout << "total=" << total << std::endl;
return 0;
}
#include <iostream>
int main() {
int base = 2;
int&& temporary = base * 2;
int total = temporary + 1;
std::cout << "temporary=" << temporary << std::endl;
std::cout << "total=" << total << std::endl;
return 0;
}
#include <iostream>
int main() {
int base = 10;
int&& temporary = base * 2;
int total = temporary + 1;
std::cout << "temporary=" << temporary << std::endl;
std::cout << "total=" << total << std::endl;
return 0;
}
base ← 6, temporary ← 12, total ← 13
3int main() {4 int base→ 6 = 6; //@base=2, 1056 int&& temporary→ 12 = base6 * 2;7 int total→ 13 = temporary12 + 1;89 std::cout << "temporary=" << temporary12 << std::endl;10 std::cout << "total=" << total13 << std::endl;11 return 0;12}outputtemporary=12 total=13
base ← 2, temporary ← 4, total ← 5
3int main() {4 int base→ 2 = 2;56 int&& temporary→ 4 = base2 * 2;7 int total→ 5 = temporary4 + 1;89 std::cout << "temporary=" << temporary4 << std::endl;10 std::cout << "total=" << total5 << std::endl;11 return 0;12}outputtemporary=4 total=5
base ← 10, temporary ← 20, total ← 21
3int main() {4 int base→ 10 = 10;56 int&& temporary→ 20 = base10 * 2;7 int total→ 21 = temporary20 + 1;89 std::cout << "temporary=" << temporary20 << std::endl;10 std::cout << "total=" << total21 << std::endl;11 return 0;12}outputtemporary=20 total=21