C++ 类型推导(第一部分)

在调用模板(函数)的时候,编译器会根据实参类型推导模板参数类型和函数参数类型,从而实例化出对应的函数。通常模板参数类型的推导结果与函数参数类型的推导结果不一样,这是因为函数参数类型会有诸如 const、reference、volatile 等修饰符。模板参数类型的推导结果 不仅取决于实参类型 还依赖函数参数类型的形式 ,本文只讨论其中两种形式------函数参数类型组成部分包含指针或引用,但不是万能引用、函数参数组成部分既没有指针也没有引用

如下代码片段 T,即模板参数类型;ParamType,为函数参数类型(形式);expr 为实参,其类型是 int。

cpp 复制代码
template<typename T>
void foo(ParamType v) {
}

int expr = 9;
foo(expr);

一、ParamType 是个引用(指针)但不是万能引用时 T 的推导规则

  1. 如果实参(expr)含有引用,先将其引用部分忽略;
  2. 然后把 expr 的类型和 ParamType的类型进行模式匹配,进而确定 T 的类型。

二、ParamType 既非指针也非引用时 T 的推导规则

  1. 如果实参(expr)含有引用,则忽略其引用部分;
  2. 忽略实参引用性之后,若实参是个const对象,就要忽略 const、同样若实参是个 volatile 对象,也要忽略 volatile。

三、推导过程举例

上文提到 ParamType 的形式决定T的推导结果,接下来一一列举 ParamType 的各种形式的推导过程:

  • ParamType 为 T& 形式

可以看到推导结果输出内容显示:T 被推到为 int 或 const int,ParamType 则对应为 int& 或 const int&。当调用者向引用类型的函数参数传入const 对象时,意味着不希望在函数体内修改该对象,所以应该将实参的常量性(constness)成为模板参数T的类型推导结果的组成部分。虽然调用者传入了带引用性(reference-ness)的实参,但是在推导过程中忽略了其引用性(reference-ness),所以模板参数T的类型推导结果中不包含引用部分。

cpp 复制代码
#include <iostream>

#include <boost/type_index.hpp>

template<typename T>
static inline void test_ref_pattern(T& v) {
	using boost::typeindex::type_id_with_cvr;
	using std::cout;
	using std::endl;

	cout << "T=" << type_id_with_cvr<T>().pretty_name()
		<< ", ParamType=" 
        << type_id_with_cvr<decltype(v)>().pretty_name() << endl;
}

int main() {
    int x = 66;
    const int cx = x;
    int& ref = x;
    const int& cref = ref;
    test_ref_pattern(x);
    test_ref_pattern(ref);
    test_ref_pattern(cx);
    test_ref_pattern(cref);
 }
  • ParamType 为 const T& 形式

当函数参数类型添加 const 属性时,模板参数的类型推导结果、函数参数的类型推导结果与上边有所差异:由于函数参数中已经包含常量性(constness),所以模板参数的类型推导结果就没包含 const 必要了。

cpp 复制代码
template<typename T>
void test_const_ref_pattern(const T &v) {
	using boost::typeindex::type_id_with_cvr;
	using std::cout;
	using std::endl;

	cout << "T=" << type_id_with_cvr<T>().pretty_name()
		<< ", ParamType=" 
        << type_id_with_cvr<decltype(v)>().pretty_name() << endl;
}

int main() 
{
    int x = 66;
    const int cx = x;
    int& ref = x;
    const int& cref = ref;

    test_const_ref_pattern(x);
    test_const_ref_pattern(ref);
    test_const_ref_pattern(cx);
    test_const_ref_pattern(cref);
}
  • ParamType 为 T 形式

这种形式下意味着实参按照"值传递"。这样即使实参有 const 属性,形参仍可不具备 const 属性,换句话说实参不能被修改并不能说明形参在函数体内不能被修改。所以实参的常量性(constness)和挥发性(volatileness)可以在推导过程中忽略。需要重点说明的是去除这两种属性仅发生在"值形参"模式时。

cpp 复制代码
template<typename T>
void test_value_pattern(T v) {
	using boost::typeindex::type_id_with_cvr;
	using std::cout;
	using std::endl;

	cout << "T=" << type_id_with_cvr<T>().pretty_name()
		<< ", ParamType=" 
        << type_id_with_cvr<decltype(v)>().pretty_name() << endl;
}

int main() 
{
    int x = 66;
    const int cx = x;
    int& ref = x;
    const int& cref = ref;

    test_value_pattern(x);
    test_value_pattern(ref);
    test_value_pattern(cx);
    test_value_pattern(cref);
}
  • 传递具有顶层const和底层const的实参传递

这种情况类型推导过程中p所指对象的常量性得以保留,p的常量性会在复制形参过程中被忽略。

cpp 复制代码
int main() 
{
    const char* const p = "fun with pointer";
    test_value_pattern(p);
 }

四、总结

这一部分的类型推导法则主要是和C++11之前版本相关的,类型推导过程中按照普通函数的传参规则对const属性进行了取舍、这些行为主要依赖函数参数的表达方式。模板参数的类型按照"最小原则"推导,也就是说T放到函数参数中能清楚表达形参类型就可以了。后续部分会继续探索C++11万能引用相关的类型推导。

相关推荐
2401_8920709816 小时前
【Linux C++ 日志系统实战】LogFile 日志文件管理核心:滚动策略、线程安全与方法全解析
linux·c++·日志系统·日志滚动
yuzhuanhei16 小时前
Visual Studio 配置C++opencv
c++·学习·visual studio
不爱吃炸鸡柳16 小时前
C++ STL list 超详细解析:从接口使用到模拟实现
开发语言·c++·list
十五年专注C++开发16 小时前
RTTR: 一款MIT 协议开源的 C++ 运行时反射库
开发语言·c++·反射
‎ദ്ദിᵔ.˛.ᵔ₎17 小时前
STL 栈 队列
开发语言·c++
2401_8920709817 小时前
【Linux C++ 日志系统实战】高性能文件写入 AppendFile 核心方法解析
linux·c++·日志系统·文件写对象
郭涤生17 小时前
STL vector 扩容机制与自定义内存分配器设计分析
c++·算法
༾冬瓜大侠༿17 小时前
vector
c语言·开发语言·数据结构·c++·算法
cccyi717 小时前
【C++ 脚手架】etcd 的介绍与使用
c++·服务发现·etcd·服务注册
liu****17 小时前
第16届省赛蓝桥杯大赛C/C++大学B组(京津冀)
开发语言·数据结构·c++·算法·蓝桥杯