《C++ Templates》:有关const、引用、指针的一些函数模板实参推导的例子

1.T按值传递

最简单的模板例子:

cpp 复制代码
template<typename T>
void func(T x) {
    std::cout << typeid(T).name() << std::endl;
    x = 20;
    cout << x;
}

这种情况下,T永远不会被推导成带顶层const或引用的类型

【顶层const即变量本身不能被修改,例如const int和const int &都是变量本身不能被修改的情况】

这种情况,T是int类型

cpp 复制代码
int a = 10;
int& b = a;
func(b);

这种情况,T还是int类型

cpp 复制代码
int a = 10;
const int& p = a;
func(p);

这种情况,T还是int类型

cpp 复制代码
const int a = 10;
func(a);

这种情况,T是int *

cpp 复制代码
int* p = nullptr;
func(p);

而这种情况,T还是int * (因为int *const p的意思是p是一个指向int类型的指针,而且指针p的值不能改变,故该const是顶层const)

cpp 复制代码
int *const p = nullptr;
func(p);

而底层const会被保留

【底层const即变量指向的内容不能被修改,典型例子是const int *p=&a,变量p的const就是底层const】

故这种情况下T是const int *(int const *)类型

cpp 复制代码
template<typename T>
void func(T x) {
    std::cout << typeid(T).name() << std::endl;
}
int main()
{
    const int *a =nullptr;
    func(a);
}

顺便提一下,这种情况T依然是const int *,因为T是不会推导出引用的

cpp 复制代码
const int* a = nullptr;
const int *&p =a;
func(p);

2.T &

T 仍然不会是引用

但是T会保留顶层const

func函数如下

cpp 复制代码
#include <type_traits>
using namespace std;
template<typename T>
void func(T &x) {
	if (std::is_reference<T>::value) {//可以判断T是否是引用类型
		std::cout << "T is a reference type." << std::endl;
	}
	else {
		std::cout << "T is not a reference type." << std::endl;
	}
    std::cout << typeid(T).name() << std::endl;//即使T真是int &,typeid(T).name()也只会输出int,所以要上面的判断帮忙
    x = 20;
    cout << x << endl;
}

T是int;x的类型是int &,运行过后a的值也是20;

cpp 复制代码
int a = 10;
int& b = a;
func(b);

T是const int;x的类型是const int &

cpp 复制代码
template<typename T>
void func(T &x) {
	if (std::is_reference<T>::value) {
		std::cout << "T is a reference type." << std::endl;
	}
	else {
		std::cout << "T is not a reference type." << std::endl;
	}
	if (std::is_const<T>::value) {//检测const
		std::cout << "T is a const type." << std::endl;
	}
	else {
		std::cout << "T is not a const type." << std::endl;
	}
    std::cout << typeid(T).name() << std::endl;
    //x = 20;由于x是const int &,不可以给x赋值
    cout << x << endl;
}
int a=10;
const int& b = a;
func(b);
相关推荐
程序员老舅2 小时前
C++音视频开发:基础面试题
c++·ffmpeg·音视频·视频编码·h264·音视频编解码·视频解码
lifallen5 小时前
深入解析RocksDB的MVCC和LSM Tree level
大数据·数据结构·数据库·c++·lsm-tree·lsm tree
君鼎5 小时前
Effective C++ 条款18:让接口容易被正确使用,不易被误用
c++
whxnchy5 小时前
C++刷题 - 7.27
开发语言·c++
白日梦想家-K6 小时前
题单【模拟与高精度】
开发语言·c++·算法
岁忧6 小时前
(LeetCode 面试经典 150 题) 138. 随机链表的复制 (哈希表)
java·c++·leetcode·链表·面试·go
君鼎6 小时前
Effective C++ 条款17:以独立语句将newed对象置入智能指针
c++
极客BIM工作室7 小时前
深入理解C++中的Lazy Evaluation:延迟计算的艺术
开发语言·c++
小指纹9 小时前
图论-最短路Dijkstra算法
数据结构·c++·算法·深度优先·图论
王德博客9 小时前
【从基础到实战】STL string 学习笔记(上)
c++·笔记·学习