c++ - 接受右值引用参数的模板函数
PHP中文网
PHP中文网 2017-04-17 13:13:30
0
3
819
#include <iostream>
#include <type_traits>
template<typename T>void g(T&& val);
int main() 
{
    int i = 0;
    const int ci = i;
    g(i*ci);//请问这里模板实例化的T是int还是int&&?我认为是int,但很多地方说是int&&,感觉不可思议
}
PHP中文网
PHP中文网

认证高级PHP讲师

reply all(3)
Peter_Zhu

If I remember correctly, T is int and val is int&&. You can read the first chapter of effective modern c++

黄舟


Write a program to verify it

Sorry, I just wrote it wrong. The following is the correct verification scheme.


The output is 10 and 999
I just want to output val in g(), the value is 10
Then I modify the val in g(), and then return When main outputs 999
, it means that the parameters here are passed by reference, not by value.
In other words, the message is T&

小葫芦

The answer is: non-const rvalue reference.
This problem is about understanding the left and right values.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template