c ++ std :: moveおよび完全な転送std :: forward



C Std Move Perfect Forwarding Std



#define _CRT_SECURE_NO_WARNINGS #include #include #include #include // There is another widely accepted saying in C++, that is, the address can be taken, the name is the left value, and vice versa, the address cannot be taken, and the name without the name is the right value. // Relative to the lvalue, the rvalue represents a literal constant, an expression, a non-reference return value of the function, and so on. // Since the compiler can only call the transfer constructor and the transfer assignment function for right-value references, all named objects can only be lvalue references. If a named object is no longer used, it is called to call the transfer constructor. And transfer assignment function, that is, use an lvalue reference as an rvalue reference. How do you do this? // The standard library provides the function std::move, which converts lvalue references to rvalue references in a very simple way. int a Int &&r1 = a // compile failed Int &&r2 = std::move(a) // compile through // Perfect forwarding std::forward // Perfect forwarding is suitable for scenarios where a set of parameters needs to be passed to another function intact. // 'Untouched' is not just the value of the parameter. In C++, in addition to the parameter values, there are two sets of properties: lvalue/right and const/non-const. // Perfect forwarding means that all of these properties and parameter values ​​cannot be changed during parameter passing, and at the same time, without incurring additional overhead, as if the forwarder does not exist. In generic functions, such requirements are very common. // How does C++11 solve the problem of perfect forwarding? In fact, C++11 accomplishes perfect forwarding by introducing a new language rule called 'reference collapsing' and combining the new template inference rules. typedef const int T typedef T & TR TR &v = 1 //In C++11, once such an expression occurs, a reference fold occurs, which collapses the complex unknown expression into a known simple expression. /* Citation folding rules in C++11: Type definition of TR Declare the type of v The actual type of v T & TR T & T & TR & T & T & TR && T & T && TR T && T && TR & T & T && TR && T && Once an lvalue reference appears in the definition, the reference fold always prioritizes it as an lvalue reference. */ // In C++11, std::forward can save the left or right value of the parameter. #include using namespace std template void process_value(T & val) { cout << 'T &' << endl } template void process_value(T && val) { cout << 'T &&' << endl } template void process_value(const T & val) { cout << 'const T &' << endl } template void process_value(const T && val) { cout << 'const T &&' << endl } //The function forward_value is a generic function that passes one argument to another function process_value Template void forward_value(T && val) //The parameter is an rvalue reference { Process_value( std::forward(val) ) // In C++11, std::forward can save the left or right value of the parameter. } void mytest() { int a = 0 const int &b = 1 forward_value(a) // T & forward_value(b) // const T & forward_value(2) // T && forward_value( std::move(b) ) // const T && return } int main() { mytest() system('pause') return 0 }

からの転送: https://www.cnblogs.com/lsgxeva/p/7787660.html