C++ के Rvalue References
In this page:
data_type &&reference_name = temporary_value;
void function_name(data_type &¶m) { } // 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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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 (&&)
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- बिना
std::moveके एक named variable से एक rvalue referenceint&&bind करने की कोशिश करना, जो compile नहीं होता। - एक object को
std::moveको pass करने के बाद इस्तेमाल करना, जब इसकी value unspecified हो सकती है। - यह मान लेना कि
std::moveखुद कुछ move करता है, जब यह सिर्फ एक rvalue में cast करता है और move constructor या assignment में होता है।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: