欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c/c++ 模板 类型推断

程序员文章站 2022-07-10 11:34:20
模板类型的推断 下面的函数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’
  */
  
}