c/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/c++ 学习互助QQ群:877684253

1414315-20181106214320230-961379709.jpg

本人微信:xiaoshitou5854

转载于:https://www.cnblogs.com/xiaoshiwang/p/10314223.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值