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 ...
随机推荐
- android 自定义权限管理
在Android6.0后有些权限就需要进行询问,虽然可以将targetSdkVersion设置成小于等于23,但是这样可能有些东西无法使用,所以要进行权限的管理. 实现逻辑:打开页面就询问权限,如果没 ...
- IconFont的iOS使用
IconFont的使用 Iconfont-国内功能很强大且图标内容很丰富的矢量图标库,提供矢量图标下载.在线存储.格式转换等功能.阿里巴巴体验团队倾力打造,设计和前端开发的便捷工具. https:// ...
- Windows Server 2016-Netdom Join加域并指定OU (一)
前边我们提到了客户端如何通过图形化.netdom .Powershell方式加域,这里我们简单补充下生产环境中如何通过有权限的用户账号加域并指定对应的OU,以防止域策略下发对部分生产服务器权限等内容进 ...
- SQLServer之删除用户自定义数据库用户
删除用户自定义数据库用户注意事项 不能从数据库中删除拥有安全对象的用户. 必须先删除或转移安全对象的所有权,才能删除拥有这些安全对象的数据库用户. 不能删除 guest 用户,但可在除 master ...
- STM32的IAP实现
STM32的IAP实现 2014年07月28日 16:31:06 Stylesen 阅读数:556 IAP,全称是“In-Application Programming”,中文解释为“在程序中编程 ...
- 在Windows上使用Docker运行.NetCore
今天我们来说下如何在windows下使用docker运行.net core,既然是docker,那么我们首先得在windows上安装docker. 在Windows安装 docker 有两种选择 :1 ...
- Java集合详解1:ArrayList,Vector与Stack
今天我们来探索一下LinkedList和Queue,以及Stack的源码. 具体代码在我的GitHub中可以找到 https://github.com/h2pl/MyTech 喜欢的话麻烦star一下 ...
- [翻译] 对正在使用EF6x开发人员的一些话
Entity Framework Core in Action Entityframework Core in action是 Jon P smith 所著的关于Entityframework Cor ...
- 使用 线性规划 解决 数字 排序问题, +Leapms模型
问题 将如下一组数字从大到小排序. {10, 20, -32, 177, 0, -11.5, 19, 7, 6.2, -6.28, -2.71, 44} 解决办法 建立数学模型,给出各个数字的次序值. ...
- SpringBoot轻松整合ElasticSearch
完整项目代码地址(https://github.com/fonxian/spring-elasticsearch-example/tree/master/spring-elasticsearch-ex ...