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
c/c++ 模板 类型推断的更多相关文章
- [Effective Modern C++] Item 1. Understand template type deduction - 了解模板类型推断
条款一 了解模板类型推断 基本情况 首先定义函数模板和函数调用的形式如下,在编译期间,编译器推断T和ParamType的类型,两者基本不相同,因为ParamType常常包含const.引用等修饰符 t ...
- 现代C++之理解模板类型推断(template type deduction)
理解模板类型推断(template type deduction) 我们往往不能理解一个复杂的系统是如何运作的,但是却知道这个系统能够做什么.C++的模板类型推断便是如此,把参数传递到模板函数往往能让 ...
- 现代C++之理解auto类型推断
理解auto类型推断 上一篇帖子中讲述了模板类型推断,我们知道auto的实现原理是基于模板类型推断的,回顾一下模板类型推断: template <typename T> void f(Pa ...
- jdk8系列二、jdk8方法引用、重复注解、更好的类型推断、新增注解
一.方法引用 方法引用使得开发者可以直接引用现存的方法.Java类的构造方法或者实例对象.方法引用和Lambda表达式配合使用,使得java类的构造方法看起来紧凑而简洁,没有很多复杂的模板代码. 方法 ...
- 【C++ Primer 第16章】2. 模板实参推断
模板实参推断:对于函数模板,编译器利用调用中的函数实参来确定模板参数,从函数实参来确定模板参数的过程被称为模板实参推断. 类型转换与模板类型参数 与往常一样,顶层const无论在形参中还是在是实参中, ...
- C++学习笔记(4)----模板实参推断
1. 如图所示代码,模板函数 compare(const T&, const T&) 要求两个参数类型要一样. compare("bye","dad&qu ...
- C++ 自动类型推断
C++语言提供了自动类型推断的机制,用于简化代码书写,这是一种很不错的特性,使用auto和decltype都可以完成自动类型推断的工作,而且都工作在编译期,这表示在运行时不会有任何的性能损耗. 一.a ...
- C++ Templates (1.2 模板实参推断 Template Argument Deduction)
返回完整目录 目录 1.2 模板实参推断 Template Argument Deduction 1.2 模板实参推断 Template Argument Deduction 当调用函数模板(如max ...
- C++11特性——变量部分(using类型别名、constexpr常量表达式、auto类型推断、nullptr空指针等)
#include <iostream> using namespace std; int main() { using cullptr = const unsigned long long ...
随机推荐
- wow.js让css3动画变动更有趣(滚动页面动画模拟懒加载特效)
CSS3的出现给网站页面增加了活力,网站增色不少,有这么小小的一款插件就能做出很多动画效果. 最重要的是它:简单易用.轻量级.无需 jQuery......他就是wow.js 地址:https://d ...
- 用samba来创建windows下的文件共享
前言 Samba是一个能让Linux系统应用Microsoft网络通讯协议的软件,而SMB是Server Message Block的缩写,即为服务器消息块 ,SMB主要是作为Microsoft的网络 ...
- navicat导入.sql文件
用Navicat for Mysql导入.sql文件 虽然这算不上什么难事,但是对于新手来说(比如说我),Navicat for MySQL里的导出连接.运行SQL文件.导入向导.还原备份.这些功 ...
- 超好用的C#控制台应用模板
本文是Wei的公众号投稿文章: 默认模板之缺 在工作学习中,我们经常需要创建一些简单的控制台应用(Console App)去验证某个想法,或者作为小工具交付给其他同事. 通常我们的选择是 Visual ...
- 主机与虚拟机都可以上网,但是互相ping不通
问题:主机与虚拟机都可以上网,但是互相ping不通 可能:相关入站规则没有启用 解决:第4步双击后,打勾设置“已启用”
- 网站被k到可以使用关键词搜索到首页优化总结
从今年二月份,刚过完年回到公司,大约一周多过后,网站就被不知名黑客攻击,然后又因为网站标题关键词堆砌导致网站被降权,从此首页不在有我的网站的踪迹,有的只是其他页面的信息,因为刚开始接触SEO,对这一块 ...
- Ubuntu 安装yii2 advanced版 遇到的坑
1.安装 Composer https://www.yiichina.com/doc/guide/2.0/start-installation通过 Composer 安装 curl -sS https ...
- dataframe行变换为列
新建一个 dataFrame : val conf = new SparkConf().setAppName("TTyb").setMaster("local" ...
- .NET Core微服务之基于Steeltoe使用Hystrix熔断保护与监控
Tip: 此篇已加入.NET Core微服务基础系列文章索引 => Steeltoe目录快速导航: 1. 基于Steeltoe使用Spring Cloud Eureka 2. 基于Steelt ...
- jQuery(四)、文档处理
1 内部插入 1.1 append(content | fn) 向每个匹配的元素内部追加内容. 参数: (1) content:要追加到目标中的内容. (2) function(index, html ...