当前位置:网站首页>Clause 30: be familiar with the failure of perfect forwarding
Clause 30: be familiar with the failure of perfect forwarding
2022-06-13 04:55:00 【CCSUZB】
“ forward ” It means that a function passes its parameters ( forward ) Just give another function . The meaning of perfect forwarding is that we not only forward objects , It also forwards its salient features : type , The left value , Right value , And whether it has const
or volation
Modifiers, etc .
Suppose there is a function f
, Then we are going to write a function that will f
As a forwarding target , Consider the following code :
template<typename T>
void fwd(T && param) {
// Accept arbitrary arguments
f(std::forward<T>(param)); // Forwarding to f
}
Given the objective function f
And forwarding functions fwd
, When with a specific argument f
Can perform an operation , And call... With the same argument fwd
Can perform different operations , It is called perfect forwarding failure :
f(experssion); // If this statement does something
fwd(experssion);// This statement performs different operations , said fwd Perfect forwarding experssion To f Failure
The following explains the arguments that cannot implement perfect forwarding one by one
Brace initializer
hypothesis f
The statement is as follows :
void f(const std::vector<int> &);
f({
1, 2, 3}); // That's all right. ,{1, 2, 3} Will implicitly convert std::vector<int>
fwd({
1, 2, 3}); // error ! Unable to compile
Above fwd
For the use of brace initializers , It's a case of perfect forwarding failure ; Via the forwarding function template fwd
Come on f
When implementing an indirect call , The compiler will no longer compare fwd
The shape parameter declared by the passed in argument at the call . Perfect forwarding fails when the following conditions are true
- The compiler cannot create a file for one or more
fwd
To derive the type result - The compiler is one or more
fwd
The formal parameters of are derived “ FALSE ” Type the results
0 and NULL Used as a null pointer
Clause 8 Said , If you try to put 0 and NULL Pass null pointer to function template , Type derivation will lead to behavior distortion , The result will be an integer . The conclusion is that :0 and NULL
Can't be used as a null pointer for perfect forwarding
Only declared integers static const
Member variables
Consider the following code :
class widget {
public:
static const std::size_t MinVals = 28; // give MinVals Statement
};
void f(std::size_t val) {
cout << "this is f fuxntion";
}
template<typename T>
void fwd(T && param) {
// Accept arbitrary arguments
f(std::forward<T>(param)); // Forwarding to f
}
std::vector<int> widgetData;
widgetData.reserve(widget::MinVals);
f(widget::MinVals); // No problem , When f(28) Handle
fwd(widget::MinVals); // error , Should not be able to link
Overloaded function name and template name
Consider the following code :
int processval(int value) {
return 0;
}
int processval(int value, int priority) {
return 1;
}
f(processval); // No problem
Above f
in call , Compilers know what they need to call processVal
. however fwd
Is that all right , Because as a function template , It doesn't have any information about type requirements , This also makes it impossible for the compiler to decide which function overloaded version should be passed
fwd(processVal); // error ! Which one? processVal Overload version ?
Bit field
void f(std::size_t sz) {
std::cout << "this is f function";
}
template<typename T>
void fwd(T &¶m) {
f(std::forward<T>(param));
}
IPv4Header h;
f(h.totalLength); // That's all right.
fwd(h.totalLength); // error ! Not const References must not be bound to domain
边栏推荐
- [JS solution] leedcode 200 Number of islands
- [LeetCode]-滑动窗口
- QT using layout manager is invalid or abnormal
- OpenCV中的saturate操作(饱和操作)究竟是怎么回事
- Section 2 - branch and loop statements
- Section 8 - Practical commissioning techniques
- Go/golang connection to database
- Shell built-in string substitution
- Mysql database installation
- Recommended temporary online image compression tool
猜你喜欢
Section 6 - pointers
Kaggle 时间序列教程
Conception d'un système basé sur MVC avec javaswing JDBC
Crawler scrapy framework learning 1
Section 3 - functions
Avantages de win8.1 et win10
Explain the differences and usage scenarios between created and mounted
约瑟夫问题
CMB's written test -- data analysis
Elliptic curve encryption
随机推荐
Internet people a few years ago vs Internet people today
On switch() case statement in C language
Implementing the driver registration initcall mechanism on stm32
Go/golang connection to database
Sampo Lock
Powershell 加域 Add-Computer模块
ES6 learning
Win8.1和Win10各自的优势
Advanced C - Section 2 - pointers
RMQ、LCA
JS to realize the conversion between string and array and an interview question
JS, how to add grid style
rust编程-链表:使用struct实现链表,使用堆合并k个升序链表,自定义Display
Tita: Xinrui group uses one-to-one talk to promote the success of performance change
Embedded hardware - read schematic
[JS solution] leedcode 200 Number of islands
如何只用4步,实现一个自定义JDBC驱动?
第三方评论插件
C#获取WebService接口的所有可调用方法[WebMethod]
Elliptic curve encryption