Win Programming Practice (5) "C + +"-rvalue reference

Source: Internet
Author: User

Rvalue reference form: Type && a= the referenced object.

The difference from an lvalue reference & is that the right value is a temporary variable that can be understood as a reference to an rvalue, and the temporary variable disappears after the right value is initialized.

From a practical point of view, it can perfectly solve the problem of temporary object efficiency which has long been criticized by C + +. From the language itself, it has a good sense of the shortcomings of reference types in C + + in terms of lvalue values. From the perspective of the Library designer, it brings a tool to the library designer. From the perspective of the library user, you can get a "free" efficiency boost by not moving a single soldier.

In the standard C + + language, a temporary amount (the term rvalue, which appears to the right of an assignment expression) can be passed to a function, but only accepted as a const & type. This way, the function cannot distinguish between a true rvalue or a regular variable passed to the const &. Also, because the type is const &, the function cannot change the value of the passed object. C++0X will add a new reference type called an rvalue reference, which is recorded as TypeName &&. This type can be accepted as a non-const value, allowing changes to its value. This change will allow some objects to create transfer semantics. For example, a std::vector, in its internal implementation, is the encapsulation of a C-type array. If you need to create a vector temp or return a vector from a function, you can store the data only by creating a new vector and copying all the data stored in the right value. The temporary vector is then destroyed, and the data it contains is deleted. With an rvalue reference, a transfer constructor for a std::vector that points to the rvalue reference of a vector can simply copy a pointer to a C-type array in that right value to the new vector and then empty the right value. There is no array copy, and destroying the emptied rvalue does not destroy the memory that holds the data. The function that returns a vector now only needs to return a std::vector<>&&. If the vector does not transfer the constructor, the result will be the same as before: its copy constructor is called with the std::vector<> & parameter. If the vector does have a transfer constructor, then the transfer constructor is called to avoid a large amount of memory allocation.


This blog all content is original, if reproduced please indicate sourcehttp://blog.csdn.net/myhaspl/

Rvalue reference

ConsoleApplication1.cpp: Defines the entry point of the console application.
Code:http://blog.csdn.net/myhaspl


#include "stdafx.h"
#include <iostream>
Using Std::endl;
Using Std::cout;
Using Std::cin;


Class mypoint{
Public
int n_x = 0;
int n_y = 0;
Code:http://blog.csdn.net/myhaspl
MyPoint (int x, int y) {
n_x = x;
n_y = y;
cout << n_x << "," << n_y;
cout << "create!" << Endl;
}
MyPoint (const mypoint &AMP;MYP) {
n_x = myp.n_x;
n_y = myp.n_y;
cout << n_x << "," << n_y;
cout << "copy constructor call!" << Endl;
}
MyPoint (MyPoint &&myp) {
n_x = myp.n_x;
n_y = myp.n_y;
cout << n_x << "," << n_y;
cout << "Move copy constructor call!" << Endl;
}
~mypoint () {
cout << n_x << "," << n_y;
cout << "delete!" << Endl;
}
};


int mydistance (const mypoint &AMP;P1, const mypoint &AMP;P2) {
cout << "mypoint& distance!" << Endl;
Return static_cast<int> (sqrt ((p2.n_x-p1.n_x) * (p2.n_x-p1.n_x) + (p2.n_y-p1.n_y) * (p2.n_y-p1.n_y));
}






int mydistance (MyPoint &&p1, MyPoint &&p2) {
cout << "mypoint&& distance!" << Endl;
Return static_cast<int> (sqrt ((p2.n_x-p1.n_x) * (p2.n_x-p1.n_x) + (p2.n_y-p1.n_y) * (p2.n_y-p1.n_y));
}




int main ()
{
int x = 10;
int y = 100;
char temp = ';
int &xleftptr = x;
Xleftptr = 20;
cout << xleftptr << Endl;




MyPoint myp1= MyPoint (10,20);
MyPoint myp2 = mypoint (100, 200);
MyPoint MYP3 = MyPoint (88, 99);
MyPoint lp3 = myp3;
MyPoint &AMP;LP1 = MYP1;
MyPoint &AMP;LP2 = MYP2;
---lvalue reference
cout << "lvalue reference result:" << mydistance (MYP1, MYP2) << Endl;
---rvalue reference
cout << "rvalue reference result:" << mydistance (MyPoint (), MyPoint (1100, 2200)) << Endl;
CIN >> Temp;
return 0;
}




This blog all content is original, if reproduced please indicate sourcehttp://blog.csdn.net/myhaspl/



20
10,20 create!
100,200 create!
88,99 create!
88, 99 copy constructor call!
mypoint& distance!
Lvalue Reference result:201
1100,2200 create!
110,220 create!
mypoint&& distance!
Rvalue reference result:2213
110,220 delete!
1100,2200 delete!

This blog all content is original, if reproduced please indicate sourcehttp://blog.csdn.net/myhaspl/



Win programming Practice (5) "C + +"-rvalue reference

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.