← Back to C++ Course | Chapter 16: Modern C++ | Lesson 5 of 9

C++ के Rvalue References

एक rvalue reference एक temporary value को पकड़ने का तरीका है जो गायब होने वाली है, गायब होने से पहले एक ball पकड़ने की तरह। यह copy करने की बजाय move करना possible बनाता है।
Syntax
cpp
data_type &&reference_name = temporary_value;

void function_name(data_type &&param) { }  // accepts temporaries

Lvalues बनाम Rvalues

एक lvalue एक expression है जो एक identifiable memory location वाले एक persistent object की ओर refer करता है, जैसे एक named variable -- आप इसका address ले सकते हैं।

एक rvalue एक temporary value है, जैसे एक literal (5) या एक function की return value, जो सिर्फ briefly exist करती है और इसका कोई stable address नहीं जिस पर आप depend कर सकें।

उदाहरण: Lvalues vs. Rvalues

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare x and set it to 5
	int x = 5;
	// Declare y and set it to x + 1
	int y = x + 1;
	// Print to the console with cout
	std::cout << y << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Rvalue Reference Syntax (&&)

एक rvalue reference type के बाद एक double ampersand (T&&) से declare होता है, और specifically named variables की बजाय temporaries से bind होता है।

यह distinct reference type ही वह है जो एक function overload को compile time पर detect करने देता है कि इसे एक temporary दिया गया जिससे resources "चुराना" safe है।

उदाहरण: Rvalue Reference Syntax (&&)

cpp
// Include std::cout and std::cin
#include <iostream>

void show(int &&x) {
	// Print to the console with cout
	std::cout << "Rvalue reference: " << x << std::endl;
}

// Program execution starts in main()
int main() {
	show(5);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Binding Constraints

एक regular lvalue reference (T&) सिर्फ एक lvalue से bind हो सकता है -- यह एक temporary से बिल्कुल bind नहीं हो सकता, क्योंकि reference safely इस्तेमाल होने से पहले temporary destroy हो सकता है।

दूसरी तरफ, एक rvalue reference (T&&) सीधे एक named variable से bind नहीं हो सकता, क्योंकि वह variable अभी भी कहीं और active use में है।

उदाहरण: Binding Constraints

cpp
// Include std::cout and std::cin
#include <iostream>

void show(int &x) { std::cout << "lvalue ref" << std::endl; }
void show(int &&x) { std::cout << "rvalue ref" << std::endl; }

// Program execution starts in main()
int main() {
	// Declare a and set it to 5
	int a = 5;
	show(a);
	show(10);
	// Return 0 to signal that the program finished successfully
	return 0;
}

std::move से Lvalues Cast करना

std::move() specifically इस gap को bridge करने के लिए exist करता है: यह एक lvalue को एक rvalue reference में cast करता है, आपको compiler को explicitly बताने देते हुए "मैं इस variable से खत्म हो गया, इसे एक temporary की तरह treat करो जिससे तुम free होकर move कर सकते हो" भले ही इसका एक नाम और एक address हो।

उदाहरण: Casting Lvalues with std::move

cpp
// Include std::cout and std::cin
#include <iostream>
#include <utility>

void show(int &&x) { std::cout << "rvalue: " << x << std::endl; }

// Program execution starts in main()
int main() {
	// Declare a and set it to 5
	int a = 5;
	// std::move transfers the resource instead of copying it
	show(std::move(a));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Rvalue References के साथ Overloading

जब एक function के lvalue-reference और rvalue-reference दोनों overloads exist करते हैं, compiler automatically temporaries और function-return values के लिए rvalue overload चुनता है, जो exactly वह चीज़ है जो move semantics को call site पर आपके कोई extra code लिखे बिना transparently kick in करने देती है।

उदाहरण: Overloading with Rvalue References

cpp
// Include std::cout and std::cin
#include <iostream>
#include <utility>

void process(const int &x) { std::cout << "lvalue overload" << std::endl; }
void process(int &&x) { std::cout << "rvalue overload" << std::endl; }

// Program execution starts in main()
int main() {
	// Declare a and set it to 5
	int a = 5;
	process(a);
	process(10);
	// std::move transfers the resource instead of copying it
	process(std::move(a));
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. बिना std::move के एक named variable से एक rvalue reference int&& bind करने की कोशिश करना, जो compile नहीं होता।
  2. एक object को std::move को pass करने के बाद इस्तेमाल करना, जब इसकी value unspecified हो सकती है।
  3. यह मान लेना कि std::move खुद कुछ move करता है, जब यह सिर्फ एक rvalue में cast करता है और move constructor या assignment में होता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.