当前位置:网站首页>Clause 28: understanding reference folding
Clause 28: understanding reference folding
2022-06-13 04:55:00 【CCSUZB】
Consider the following code :
template<typename T>
void func(T &&parem);
Widget widgetFactory(); // A function that returns an R-value
Widget w; // Variable ( The left value )
func(w); // call func And pass in an lvalue ,T The derivation type of is Widget&
func(widgetFactor()); // call func And pass in the right value ,T The derivation type of is Widget
The passed arguments are of type Widget
, A left value , An R-value , This results in a template parameter T
Different derivation results are obtained . A mechanism for determining whether universal references become lvalues or lvalues , It's also std::forward
The mechanism that works .
stay C++
in “ Referenced references ” It's illegal. :
int x;
auto& & rx = x; // error ! A reference to a reference cannot be declared
When an lvalue is passed to a universal reference, the following happens :
template<typename T>
void func(T &&parem);
func(w);
If you put T
Type of derivation ( namely Widget &
) Code instantiation template , The following results are obtained :
void func(Widget& && param);
The above is a reference to the reference . Due to universal citation param
Is initialized by an lvalue , The derivation result type should be an lvalue reference , But what did the compiler do to get the following function signatures ?
void func(Widget ¶m);
The answer is quotation folding , The rule is :
If any reference is an lvalue reference , Then the result is an lvalue reference , Otherwise, it is an R-value reference
Reference folding is to make std::forward
The key to being able to work :
template<typename T>
void f(T &&fParam) {
someFunc(std::forward<T>(fParam)); // Will fParam Forward to somFunc
}
std::forward
Task for : If and only if encoding T
The information in indicates that the argument is passed an R-value , Yes fParam
( The left value ) Implement cast to right value
Reference folding also happens auto
Type generation of variables :
Widget w;
auto&& w1 = w;
initialization w1
Is an lvalue , therefore auto
The type derivation result of is Widget &
. stay w1
In the declaration Widget&
Into the auto, The following code is generated for a long time :
Widget& && w1 = w;
When the reference is folded, it will become :
Widget & w1 = w;
The third case of reference folding is survival and use typedef
And alias declaration :
template<typename T>
class Widget {
public:
typedef T&& RvalueRefToT;
}
Suppose we instantiate with a sitting value reference :
Widget<int &> w;
stay Widget China and Israel int&
Into the T
The position of gets as follows :
typedef int & &&RvalueRefToT;
The reference is collapsed as :
typedef int& RvalueRefToTl;
边栏推荐
- OpenCV中的saturate操作(饱和操作)究竟是怎么回事
- Avantages de win8.1 et win10
- 利用Javeswingjdbc基於mvc設計系統
- 2022 oxidation process operation certificate examination question bank and simulation examination
- Solution to sudden font change in word document editing
- Gradient descent, learning rate
- Sampo Lock
- Reductive elimination
- OJ problem solution summary
- Tita绩效宝:远程一对一面谈的问题
猜你喜欢
Explain the differences and usage scenarios between created and mounted
Converting MySQL data to PostgreSQL with Navicat
Reductive elimination
QT client development -- driver loading problem of connecting to MySQL database
Design system based on MVC using javeswingjdbc
QT interface rendering style
Advanced C - Section 3 - character functions and string functions
[LeetCode]-二分查找
Stepping on a horse (one stroke)
Analysis on the usage, response and global delivery of provide/inject
随机推荐
PHP syntax
[LeetCode]-二分查找
PostgreSQL Guide: Insider exploration (Chapter 7 heap tuples and index only scanning) - Notes
Stepping on a horse (one stroke)
Cesium:CesiumLab制作影像切片与切片加载
PHP development 14 compilation of friendship link module
What is the difference between ROM, ram and flash? SRAM、DRAM、PROM、EPROM、EEPROM
All blog navigation
Force buckle 25 A group of K flipped linked lists
Kaggle time series tutorial
Shell variable learning notes
[JS solution] leedcode 117 Populate the next right node pointer II for each node
Third party comment plugin
Conception d'un système basé sur MVC avec javaswing JDBC
Section 2 - branch and loop statements
约瑟夫问题
Basic syntax example for go
[leetcode]- sliding window
Win8.1和Win10各自的優勢
Analysis of the principle of V-model and its application in user defined components