当前位置:网站首页>Perfect forwarding implementation mechanism
Perfect forwarding implementation mechanism
2022-04-22 20:17:00 【sameTimer】
List of articles
Preface
To understand the right value reference , We must first understand what are left and right values , And the difference between right value and right value reference .C++ You can use... For expressions in 2 Distinguish between two independent characteristics , Value category and type , among Value category Including lvalues 、 Right value, etc , type Common data types . In this paper, we consider that left - value reference and right - value reference are one type of variables , The following are my understanding and the conclusions of the experiment .
One 、 Left and right
Left and right values are not C++ New concept of , In fact, C There is in language . It is believed that the left value can appear to the left and right of the equal sign , The right value can only appear on the right . It is easy to understand that what can take the address is the lvalue , What can't get the address is the right value .
Two 、 L-value reference and right value reference
In order to understand the principle of the concept of mobile semantics introduced later, that is, perfect forwarding , It must be based on the following consensus ,
lvalue reference : Lvalue references can only be bound to lvalues , Lvalue reference must be lvalue property
Right quoting : An R-value reference can only be bound to an R-value , An R-value reference can be an R-value attribute or an l-value attribute , And once the right value reference is bound to the right value, it is an lvalue .
The following is the experiment :
Use the template to build a template function to judge the left and right values, as follows :
void LrJudge(int& t) {
cout << "T&" << endl;
}
void LrJudge(int&& t) {
cout << "T&&" << endl;
}
Write a few examples to test :
int main()
{
int a = 1;
int& b = a;
int&& c = 1;
LrJudge(a);
LrJudge(b);
LrJudge(c);
LrJudge(1);
return 0;
}
The operation results are as follows :

Above a, b, c , 1 except 1 Is a right value, and the rest are left values , Even if the variable c It's right quoting , As mentioned above, an R-value reference, once bound, is an l-value , Because the reference must initialize the binding , Therefore, the right value references with names are all left values .
3、 ... and 、 Mobile semantics
C++ The idea of providing mobile semantics is very simple , Is to set the right value , Generally, the resource pointer of the object to be destroyed is transferred directly , Instead of using copy constructs for deep copy , Similar to shallow copy . This is not the point of this article , You can search more online if you are interested .
Four 、 Perfect forwarding
The concept of perfect forwarding is very simple , That is, perfectly forward the original type and value type of function arguments inside the function . That is, if the function passes in an argument A Is an lvalue reference type and the value type is lvalue , When forwarding, keep A Lvalue attribute and lvalue reference type of . The key to perfect forwarding lies in the following two points ,
(1) How to keep the reference type unchanged
(2) How to ensure that the value attribute remains unchanged
A perfect forwarding is realized as follows , Its purpose is to hope TestForward A function that can perfectly forward formal parameters v The value attribute and type of the corresponding argument , The argument is emphasized because , The formal parameter must be an lvalue attribute .
template<typename T>
T&& myforward(std::remove_reference_t<T>& a) noexcept
{
func(a);
return static_cast<T&&>(a);
}
template<typename T>
T&& myforward(std::remove_reference_t<T>&& a) noexcept
{
return static_cast<T&&>(a);
}
void TestForward(int&& v)
{
LrJudge(myforward<int>(v));
}
int main()
{
int a = 1;
int& b = a;
int&& c = 1;
TestForward(a);
TestForward(b);
TestForward(c);
TestForward(1);
return 0;
}

As shown above TestForward The function perfectly forwards the argument type a, b, c, 1.
(1) Lvalue references can only bind lvalues , An R-value reference can only bind an R-value , How to use only one function to receive lvalue reference and lvalue reference , Careful must find TestForwad It's a template function , If used directly int Substitution rule TestForward Only right values can be received . Template functions are T&& It is no longer an R-value reference , It's a universal reference . Collapse rules by reference T&& If the argument of is an lvalue reference, then T&& It's an lvalue quote , If the argument is an R-value reference, it is an R-value reference . This solves the first key point
(2) Because the formal parameter must be an lvalue , Therefore, it needs to be converted to the corresponding right value . According to my experimental results ,static_cast<int&>(A) The return value type of is int& Left value of ,static_cast<int&&>(A) The return value type of is int&& The right value . In fact, if you declare that the return value of the function is int& and int&& The left and right values of the corresponding type are also obtained .myforward Why std::remove_reference_t instead of T The reason is that T&& It's universal quotation , So you can't match the right value reference .
summary
It mainly summarizes the principle of perfect forwarding .
版权声明
本文为[sameTimer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221913279768.html
边栏推荐
- Which futures company is safer to open a domestic futures account?
- How to execute stored procedures in Navicat?
- What is the reason why the camera device with built-in 4G card of Haikang cannot register with easycvr platform?
- (L2-026)小字辈(带权并查集)
- C# Process运行cmd命令的异步回显
- "Shandong University project training" Research on radiation pre calculation rendering and post-processing noise reduction system (I)
- An error occurs when starting Kingbase stand-alone service: invalid value for parmeter "bindpuplist": "0-95"
- Some operations of MySQL 5.6 under Linux
- In 2022, the most underestimated function of postman, the efficiency of automated interface testing, is simply invincible
- Filebeat
猜你喜欢
![[H5] wechat H5 page production](/img/24/944d8b79fcb69614b24c2fd541ac9a.png)
[H5] wechat H5 page production

Why do I suggest you work in a technical position instead of a clerical position, sales

Advanced IPC - DBUS details

Format for creating Zimbra LDAP users using LDAP clients

Implementation of calico official website network topology: Based on ENSP and vmvare

Connection method of Jincang database kingbasees

手写一个网关服务,理解更透彻!
![[unity] pit records of Luna playable plug-ins that can play advertisements](/img/07/eda46902407ae8a3a5840b9cfb63c1.png)
[unity] pit records of Luna playable plug-ins that can play advertisements

SCI/SSCI期刊列表已更新,这几本期刊被剔除~

IAP之boot实现
随机推荐
文件上传问题记录
Acrobat Pro DC tutorial, how to use password to protect PDF files?
资料员考试题型有哪些怎么备考建设厅资料员考试
This is why deep learning is so powerful
Method of database startup installer in kingbasees
"Shandong University project training" Research on radiation pre calculation rendering and post-processing noise reduction system (I)
普通函数做友元(用举例了解友函数)
Hard work alone is not enough! And this
Write a gateway service, understand more thoroughly!
396. 旋转函数(数学规律 + 迭代法)
Why do I suggest you work in a technical position instead of a clerical position, sales
day29
启动kingbase单机服务时报错:invalid value for parmeter “bindcpulist”:”0-95”
Solve the problem that kingbasees cannot open the lock file
An error occurs when starting Kingbase stand-alone service: invalid value for parmeter "bindpuplist": "0-95"
金仓数据库KingbaseES之null和“ ”的区别
Internet News: Lenovo announced the new progress of ESG; Excellent sound and painting of Jimi h3s and z6x Pro were highly praised; Little red book responded to "layoffs of 20%"
JMeter: an introduction to the basic knowledge of 2000 word interface testing
Introduction notes to PHP zero Foundation (11): String
Kingbasees service startup method of Jincang database