模板类型的推断

下面的函数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’

实验代码

  1. #include <iostream>
  2. template<typename T>
  3. void f1(T t){
  4. t = 99;
  5. }
  6. template<typename T>
  7. void f4(const T t){
  8. //t = 99;
  9. }
  10. template<typename T>
  11. void f2(T& t){
  12. //t = 99;
  13. }
  14. template<typename T>
  15. void f3(const T& t){}
  16. template<typename T>
  17. void f5(T&& t){}
  18. template<typename T>
  19. void f6(const T&& t){}
  20. int main(){
  21. /*
  22. //void f1(T t)
  23. f1(1);//int
  24. int i = 1;
  25. f1(i);//int
  26. int& ri = i;
  27. f1(ri);//int
  28. std::cout << ri << std::endl;
  29. const int ci = 2;
  30. f1(ci);//int
  31. const int& rci = ci;
  32. f1(rci);//int
  33. */
  34. /*
  35. //void f4(const T t)
  36. f4(1);//int
  37. int i = 1;
  38. f4(i);//int
  39. int& ri = i;
  40. f4(ri);//int
  41. std::cout << ri << std::endl;
  42. const int ci = 2;
  43. f4(ci);//int
  44. const int& rci = ci;
  45. f4(rci);//int
  46. */
  47. /*
  48. //void f2(T& t)
  49. //f2(1);//error cannot bind non-const lvalue reference of type ‘int&’ to an rvalue of type ‘int’
  50. int i = 1;
  51. f2(i);//int
  52. int& ri = i;
  53. f2(ri);//int
  54. const int ci = 2;
  55. f2(ci);//const int
  56. const int& rci = ci;
  57. f2(rci);//const int
  58. */
  59. /*
  60. //void f3(const T& t)
  61. f3(1);//int
  62. int i = 1;
  63. f3(i);//int
  64. int& ri = i;
  65. f3(ri);//int
  66. const int ci = 2;
  67. f3(ci);//int
  68. const int& rci = ci;
  69. f3(rci);//int
  70. */
  71. /*
  72. //void f5(T&& t)
  73. f5(1);//int
  74. int i = 1;
  75. f5(i);//int&
  76. int& ri = i;
  77. f5(ri);//int&
  78. std::cout << ri << std::endl;
  79. const int ci = 2;
  80. f5(ci);//const int&
  81. const int& rci = ci;
  82. f5(rci);//const int&
  83. */
  84. /*
  85. //void f6(const T&& t)
  86. f6(1);//int
  87. int i = 1;
  88. //f6(i);//error cannot bind rvalue reference of type ‘const int&&’ to lvalue of type ‘int’
  89. int& ri = i;
  90. //f6(ri);//error cannot bind rvalue reference of type ‘const int&&’ to lvalue of type ‘int’
  91. std::cout << ri << std::endl;
  92. const int ci = 2;
  93. //f6(ci);//error cannot bind rvalue reference of type ‘const int&&’ to lvalue of type ‘const int’
  94. const int& rci = ci;
  95. //f6(rci);//error cannot bind rvalue reference of type ‘const int&&’ to lvalue of type ‘const int’
  96. */
  97. }

c/c++ 学习互助QQ群:877684253

本人微信:xiaoshitou5854

c/c++ 模板 类型推断的更多相关文章

  1. [Effective Modern C++] Item 1. Understand template type deduction - 了解模板类型推断

    条款一 了解模板类型推断 基本情况 首先定义函数模板和函数调用的形式如下,在编译期间,编译器推断T和ParamType的类型,两者基本不相同,因为ParamType常常包含const.引用等修饰符 t ...

  2. 现代C++之理解模板类型推断(template type deduction)

    理解模板类型推断(template type deduction) 我们往往不能理解一个复杂的系统是如何运作的,但是却知道这个系统能够做什么.C++的模板类型推断便是如此,把参数传递到模板函数往往能让 ...

  3. 现代C++之理解auto类型推断

    理解auto类型推断 上一篇帖子中讲述了模板类型推断,我们知道auto的实现原理是基于模板类型推断的,回顾一下模板类型推断: template <typename T> void f(Pa ...

  4. jdk8系列二、jdk8方法引用、重复注解、更好的类型推断、新增注解

    一.方法引用 方法引用使得开发者可以直接引用现存的方法.Java类的构造方法或者实例对象.方法引用和Lambda表达式配合使用,使得java类的构造方法看起来紧凑而简洁,没有很多复杂的模板代码. 方法 ...

  5. 【C++ Primer 第16章】2. 模板实参推断

    模板实参推断:对于函数模板,编译器利用调用中的函数实参来确定模板参数,从函数实参来确定模板参数的过程被称为模板实参推断. 类型转换与模板类型参数 与往常一样,顶层const无论在形参中还是在是实参中, ...

  6. C++学习笔记(4)----模板实参推断

    1. 如图所示代码,模板函数 compare(const T&, const T&) 要求两个参数类型要一样. compare("bye","dad&qu ...

  7. C++ 自动类型推断

    C++语言提供了自动类型推断的机制,用于简化代码书写,这是一种很不错的特性,使用auto和decltype都可以完成自动类型推断的工作,而且都工作在编译期,这表示在运行时不会有任何的性能损耗. 一.a ...

  8. C++ Templates (1.2 模板实参推断 Template Argument Deduction)

    返回完整目录 目录 1.2 模板实参推断 Template Argument Deduction 1.2 模板实参推断 Template Argument Deduction 当调用函数模板(如max ...

  9. C++11特性——变量部分(using类型别名、constexpr常量表达式、auto类型推断、nullptr空指针等)

    #include <iostream> using namespace std; int main() { using cullptr = const unsigned long long ...

随机推荐

  1. 45个值得收藏的 CSS 形状

    摘要: CSS炫技. 原文:45个值得收藏的 CSS 形状 作者:前端小智 Fundebug经授权转载,版权归原作者所有. CSS能够生成各种形状.正方形和矩形很容易,因为它们是 web 的自然形状. ...

  2. happens-before 理解

    从JDK 5 开始,JMM就使用happens-before的概念来阐述多线程之间的内存可见性.在JMM中,如果一个操作执行的结果需要对另一个操作可见, 那么这2个操作之间必须要存在happens-b ...

  3. 分布式理论 之 CAP 定理

    -----------------------------------------------------入巷间吃汤面 笑看窗边飞雪. 目录: 什么是 CAP 定理 为什么只能 3 选 2 能不能解决 ...

  4. Python爬虫10-页面解析数据提取思路方法与简单正则应用

    GitHub代码练习地址:正则1:https://github.com/Neo-ML/PythonPractice/blob/master/SpiderPrac15_RE1.py 正则2:match. ...

  5. javascript放大镜效果

    JS实现放大镜效果 首先我们先设想一下放大镜效果 1.当鼠标进入小盒子的时候,把大图片显示出来 2.当指定移动区域的时候,显示当前放大区域(放大效果) 3.鼠标移除我们让它消失 一.实现页面布局HTM ...

  6. Linux下安装vmtools的语句

    sudo apt-get upgrade sudo apt-get install open-vm-tools-desktop -y sudo reboot 出现提示信息不确定就默认就好,一路y或ye ...

  7. RIpng配置(GNS3)(第九组)

    一.拓扑图 二.路由器配置 路由器R1的配置(配置RIP静态路由协议,路由器对应接口配置相对应的ipv6地址,并给每个接口配置RIP 1 enable) 路由器R2的配置(配置RIP静态路由协议,路由 ...

  8. 从壹开始前后端分离【 .NET Core2.0 +Vue2.0 】框架之十一 || AOP自定义筛选,Redis入门 11.1

    代码已上传Github+Gitee,文末有地址 书说上文<从壹开始前后端分离[ .NET Core2.0 Api + Vue 2.0 + AOP + 分布式]框架之十 || AOP面向切面编程浅 ...

  9. 分布式基础通信协议:paxos、totem 和 gossip(转载)

    背景: 在分布式中,最难解决的一个问题就是多个节点间数据同步问题.为了解决这样的问题,涌现出了各种奇思妙想.只有在解决了如何进行信息同步的基础之上才衍生出形形色色的应用.这里开始介绍几种分布式通信协议 ...

  10. [SpringBoot guides系列翻译]调用RESTfulWebService

    原文 参考链接 CommandLineRunner Bean 翻译如何调用RESTful WebService 这节将演示如何在SpringBoot里面调用RESTful的WebService. 构建 ...