C++ Templates (1.2 模板实参推断 Template Argument Deduction)
1.2 模板实参推断 Template Argument Deduction
当调用函数模板(如max())时,模板参数由传入的实参决定。如果传递两个int给参数类型T,C++编译器推断出T的类型为int。
然而,T可能是类型的一部分。比如说,如果声明max()使用常量引用(const reference):
template <typename T>
T max(T const& a, T const& b)
{
return b < a ? a : b;
}
并且传递int类型的参数,T同样被推断为int类型,因为函数参数与int const&完全匹配。
类型推断时类型转换
在类型推断中,自动类型转换受到限制:
当声明调用参数(call parameter)为引用类型,甚至一般的转换也不能用于类型推断。两个声明为同一个模板参数(template parameter)T类型的实参必须严格匹配。(Two arguments declared with the same template parameter T must match exactly)。
当声明调用参数为值类型,只有退化(decay)的普通转换才被支持。const或者volatile等限制将被忽略,引用类型转化为被引用的类型,原始数组(raw array)或者函数被转化为对应的指针类型。两个被声明为同一个模板参数T类型的实参,其退化类型必须匹配。
比如:
template <typename T>
T max(T a, T b);
...
int i = 17;
int const c = 42;
max(i, c); //OK: T 被推断为int
max(c, c); //OK: T 被推断为int
int& ir = ;
max(i, ir); //OK: T被推断为int
int arr[4];
max(&i, arr); //OK: T被推断为int*
然而,以下调用将引发错误:
max(4, 7.2); //Error:T 可能被推断为int或者double
std::string s;
max("hello", s); //Error: T 可能被推断为char const[6]或者std::string
有三种方法来处理此类错误:
- 转换实参使得两个参数得以匹配:
max(static_cast<double>(4), 7.2);
- 显式指定或者限制T的类型来防止编译器进行类型推断:
max<double>(4, 7.2);
- 将两个参数指定为不同的类型。
第1.3节将围绕该方案,第7.2节和第15章将详细讨论类型推断过程中的类型转换规则。
默认实参的类型类型推断 Type Deduction for Default Arguments
默认调用实参(default call arguments)不能用于类型推断,比如:
template <typename T>
void f(T = "");
...
f(1); //OK: T推断为int,因此它将调用f<int>(1)
f(); //Error: 无法推断T类型
为了支持这一情形,必须声明默认模板实参(default argument for the template parameter),这将在1.4节中讨论:
template <typename T = std::string>
void f(T = "");
...
f(); //OK
C++ Templates (1.2 模板实参推断 Template Argument Deduction)的更多相关文章
- C++ Templates (1.4 默认模板实参 Default Template Arguments)
返回完整目录 目录 1.4 默认模板实参 Default Template Arguments 1.4 默认模板实参 Default Template Arguments 可以为模板参数定义默认值,这 ...
- 【C++ Primer 第16章】2. 模板实参推断
模板实参推断:对于函数模板,编译器利用调用中的函数实参来确定模板参数,从函数实参来确定模板参数的过程被称为模板实参推断. 类型转换与模板类型参数 与往常一样,顶层const无论在形参中还是在是实参中, ...
- C++学习笔记(4)----模板实参推断
1. 如图所示代码,模板函数 compare(const T&, const T&) 要求两个参数类型要一样. compare("bye","dad&qu ...
- C++ Templates(1.3 多模板参数 Multiple Template Parameters)
返回完整目录 目录 1.3 多模板参数 Multiple Template Parameters 1.3.1 为返回类型设置模板参数参数 Template Parameters for Return ...
- [Effective Modern C++] Item 1. Understand template type deduction - 了解模板类型推断
条款一 了解模板类型推断 基本情况 首先定义函数模板和函数调用的形式如下,在编译期间,编译器推断T和ParamType的类型,两者基本不相同,因为ParamType常常包含const.引用等修饰符 t ...
- 现代C++之理解模板类型推断(template type deduction)
理解模板类型推断(template type deduction) 我们往往不能理解一个复杂的系统是如何运作的,但是却知道这个系统能够做什么.C++的模板类型推断便是如此,把参数传递到模板函数往往能让 ...
- C++ template —— 实例化和模板实参演绎(四)
本篇讲解实例化和模板实参演绎-------------------------------------------------------------------------------------- ...
- ES - Index Templates 全局index模板
1.Index Templates 之前我们聊过Dynamic template,它作用范围是特定的Index,如果我们想针对全局Index进行设置该如何操作呢? Index Templates 可以 ...
- C++中decltype(*)作为模板实参时的隐藏问题
在函数模板中使用智能指针时,可能会希望根据指针的类型推导出指针引用的对象类型作为模板参数,于是写出以下代码: shared_ptr<decltype(*objPtr)>(objPtr); ...
随机推荐
- 水题----根据O出现次数判断分数
There is an objective test result such as \OOXXOXXOOO". An `O' means a correct answer of a prob ...
- O、Θ、Ω、o、ω,别再傻傻分不清了!
前言 本篇文章收录于专辑:http://dwz.win/HjK,点击解锁更多数据结构与算法的知识. 你好,我是彤哥,一个每天爬二十六层楼还不忘读源码的硬核男人. 前面几节,我们一起学习了算法的复杂度如 ...
- Letex中表格问题
最近在学习使用Letex,在学习过程中碰到很多小问题,故记之. 以下是一个参数表的实例(绘成三线表的形式). \begin{table}[hp] %%参数: h:放在此处 t:放在顶端 b:放在底端 ...
- Seaborn基础2
import matplotlib.pyplot as plt import seaborn as sns import numpy as np def sinplot(flip = 1): x = ...
- UDP 绑定信息
""" 建立->绑定本地ip地址和端口号->接收数据->转码输出->关闭客户端 """ from socket im ...
- Numpy改变数组的形状
import numpy as np n = np.arange(10) # array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) # 查看数组的大小 n.size # # 将数 ...
- PHP 数据库 ODBC创建 ODBC 连接
PHP 数据库 ODBC ODBC 是一种应用程序编程接口(Application Programming Interface,API),使我们有能力连接到某个数据源(比如一个 MS Access 数 ...
- PHP is_scalar() 函数
is_scalar() 函数用于检测变量是否是一个标量.高佣联盟 www.cgewang.com 标量变量是指那些包含了 integer.float.string 或 boolean 的变量,而 ar ...
- PHP atan() 函数
实例 通过 atan() 函数返回不同数的反正切: <?phpecho(atan(0.50) . "<br>");echo(atan(-0.50) . " ...
- PHP hypot() 函数
实例 计算不同的直角三角形的斜边长度: <?phpecho hypot(3,4) . "<br>";echo hypot(4,6) . "<br& ...