Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What does "int&& a = something;" in C++ means?

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 –

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading