I see at many places the fastest solution to a problem(often complex ones) sometimes have this line –
"int&& a = something;"
what does it really mean?
To test, i tried "int&& a = 5;" against "int b = 5;", and both a and b print same value 5.
However, in disassembly, i see –
void function() {
int&& a = 5;
int b = 5;
}
as
1 function():
2 push rbp
3 mov rbp, rsp
4 mov DWORD PTR [rbp-16], 5
5 lea rax, [rbp-16]
6 mov QWORD PTR [rbp-8], rax
7 mov DWORD PTR [rbp-12], 5
8 nop
9 pop rbp
10 ret
(lines 4, 5, 6 to int&&)
3 line instructions against 1.
So, what operations is it performing, and how does it makes code faster?
Searched int&& online, but could not find any reference, simple example explaining this.
>Solution :
That is a type different from your commonly seen single-ampersand lvalue reference, called an rvalue reference.
It’s used when an expensive deep-copy operation is unnecessary and could be better optimized with move semantics.
https://en.wikipedia.org/wiki/C%2B%2B11#Rvalue_references_and_move_constructors