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

本人微信:xiaoshitou5854

原文地址:https://www.cnblogs.com/xiaoshiwang/p/10314223.html