c/c++ 模板 类型推断
程序员文章站
2022-04-28 13:55:16
模板类型的推断 下面的函数f是个模板函数,typename T。下表是,根据调用测的实参,推断出来的T的类型。 请注意下表的红字部分, f(T&& t)看起来是右值引用,但其实它会根据实参的类型,来决定T的类型,如果实参是左值,则它是左值,如果实参是右值,则它是右值。 所以可以看出来,T&可以变成c ......
模板类型的推断
下面的函数f是个模板函数,typename t。下表是,根据调用测的实参,推断出来的t的类型。
请注意下表的红字部分, f(t&& t)看起来是右值引用,但其实它会根据实参的类型,来决定t的类型,如果实参是左值,则它是左值,如果实参是右值,则它是右值。
所以可以看出来,t&可以变成const& ,f(t&& t)也可以变成const&。
f(t t) | f(const t t) | f(t& t) | f(const t& t) | f(t&& t) | f(const t&& t) | |
---|---|---|---|---|---|---|
f(1) | f<int> | f<int> | error cannot bind non-const lvalue reference of type ‘int&’ to an rvalue of type ‘int' | f<int> | f<int> | f<int> |
int i = 1; f(i); |
f<int> | f<int> | f<int> | f<int> | f<int&> | error cannot bind rvalue reference of type ‘const int&&’ to lvalue of type ‘int’ |
int& ri = i; f(ri); |
f<int> | f<int> | f<int> | f<int> | f<int&> | cannot bind rvalue reference of type ‘const int&&’ to lvalue of type ‘int’ |
const int ci = 2; f(ci); |
f<int> | f<int> | f<const int> | f<int> | f<const int&> | cannot bind rvalue reference of type ‘const int&&’ to lvalue of type ‘const int’ |
const int& rci = ci; f(rci); |
f<int> | f<int> | f<const int> | f<int> | f<const int&> | cannot bind rvalue reference of type ‘const int&&’ to lvalue of type ‘const int’ |
实验代码
#include <iostream> template<typename t> void f1(t t){ t = 99; } template<typename t> void f4(const t t){ //t = 99; } template<typename t> void f2(t& t){ //t = 99; } template<typename t> void f3(const t& t){} template<typename t> void f5(t&& t){} template<typename t> void f6(const t&& t){} int main(){ /* //void f1(t t) f1(1);//int int i = 1; f1(i);//int int& ri = i; f1(ri);//int std::cout << ri << std::endl; const int ci = 2; f1(ci);//int const int& rci = ci; f1(rci);//int */ /* //void f4(const t t) f4(1);//int int i = 1; f4(i);//int int& ri = i; f4(ri);//int std::cout << ri << std::endl; const int ci = 2; f4(ci);//int const int& rci = ci; f4(rci);//int */ /* //void f2(t& t) //f2(1);//error cannot bind non-const lvalue reference of type ‘int&’ to an rvalue of type ‘int’ int i = 1; f2(i);//int int& ri = i; f2(ri);//int const int ci = 2; f2(ci);//const int const int& rci = ci; f2(rci);//const int */ /* //void f3(const t& t) f3(1);//int int i = 1; f3(i);//int int& ri = i; f3(ri);//int const int ci = 2; f3(ci);//int const int& rci = ci; f3(rci);//int */ /* //void f5(t&& t) f5(1);//int int i = 1; f5(i);//int& int& ri = i; f5(ri);//int& std::cout << ri << std::endl; const int ci = 2; f5(ci);//const int& const int& rci = ci; f5(rci);//const int& */ /* //void f6(const t&& t) f6(1);//int int i = 1; //f6(i);//error cannot bind rvalue reference of type ‘const int&&’ to lvalue of type ‘int’ int& ri = i; //f6(ri);//error cannot bind rvalue reference of type ‘const int&&’ to lvalue of type ‘int’ std::cout << ri << std::endl; const int ci = 2; //f6(ci);//error cannot bind rvalue reference of type ‘const int&&’ to lvalue of type ‘const int’ const int& rci = ci; //f6(rci);//error cannot bind rvalue reference of type ‘const int&&’ to lvalue of type ‘const int’ */ }
推荐阅读
-
C++入门到理解阶段二基础篇(3)——C++数据类型
-
c++ string类型成员变量在调用构造函数后未能正确赋值
-
C++数据类型(data type)介绍
-
c/c++编程排坑(1)-- 数据类型的“安静”转换
-
C++雾中风景11:厘清C++类型转换(static_cast,dynamic_cast,reinterpret_cast,const_cast)
-
c++ 模板类,方法返回值类型是typedef出来的,或者是auto,那么此方法在类外面如何定义?
-
C、C++的Makefile模板
-
C++负数类型转换,-1对256取模
-
c/c++ 函数模板初探
-
跟我一起学C++之从C到C++(bool类型)