C++ 模板参数展开

C++ 模板参数展开


一、获取可变参数大小

背景:

FLen<int, char, long> Len;

我想要获取模板参数类型的总大小

cpp 复制代码
template<typename T,typename ...ParamTypes>
class FLen
{
public:
	enum
	{
		Number = FLen<T>::Number + FLen<ParamTypes...>::Number
	};
};

template<typename Last>
class FLen<Last>
{
public:
	enum
	{
		Number = sizeof(Last)
	};
};

思想还是类似递归调用的思想,只是递归的不是函数而是模板

cpp 复制代码
int main()
{
	FLen<int, char, long> Len;

	std::cout << Len.Number << std::endl;

	system("pause");
	return 0;
}

二、通过模版循环继承的方式来展开可变参数

最终目的是构建一个 TestIndex 类型,其模板参数是从0开始到N-1的整数序列。

cpp 复制代码
template<int...>
struct TestIndex
{

};

template<int N,int...ParamTypes>
struct FSpawnIndex : FSpawnIndex<N - 1,N - 1,ParamTypes...>
{

};

template<int...ParamTypes>
struct FSpawnIndex<0,ParamTypes...>
{
	typedef TestIndex<ParamTypes...> Type;
};

解释一下原理和流程

起始:FSpawnIndex<3>(此时 N=3,参数包为空)

继承:FSpawnIndex<3> : FSpawnIndex<2,2>

在内部,参数包变为 2

下一步:FSpawnIndex<2,2> : FSpawnIndex<1,1,2>

参数包变为 1,2(注意:每次递归在参数包头部添加)

下一步:FSpawnIndex<1,1,2> : FSpawnIndex<0,0,1,2>

参数包变为 0,1,2

匹配终止条件:FSpawnIndex<0,0,1,2>

定义 Type 为 TestIndex<0,1,2>

cpp 复制代码
int main()
{
	using TestType = FSpawnIndex<3>::Type;

	std::cout << typeid(TestType).name() << std::endl;

	system("pause");
	return 0;
}

三、改用Using去实现循环继承

cpp 复制代码
template<int...>
struct TestIndex
{

};

template<int N,int...ParamTypes>
struct FSpawnIndex
{
	using Type = typename FSpawnIndex<N - 1, N - 1, ParamTypes...>::Type;
	//          ↑↑↑↑↑
	// 这个 typename 必不可少!
};

template<int...ParamTypes>
struct FSpawnIndex<0, ParamTypes...>
{
	typedef TestIndex<ParamTypes...> Type;
};

在 C++ 模板元编程中,typename 关键字在这里起着​​关键作用​​,主要用于解决​​依赖名称的解析问题​​。

cpp 复制代码
int main()
{
	using TestType = FSpawnIndex<3>::Type;

	std::cout << typeid(TestType).name() << std::endl;

	system("pause");
	return 0;
}

​​什么是依赖名称?​​

FSpawnIndex<N-1, ...>::Type 是​​依赖于模板参数 N 和 ParamTypes... 的名称​​

编译器在解析模板时,无法确定 ::Type 是什么(可能是类型、静态成员或嵌套模板)

例如以下例子

cpp 复制代码
// 情况分析:
struct FSpawnIndex</*...*/> {
    // 可能1:Type 是类型(typedef/using)
    typedef ... Type;

    // 可能2:Type 是静态成员
    static int Type;

    // 可能3:Type 是嵌套模板
    template<...> class Type;
};
相关推荐
AI+程序员在路上6 分钟前
Qt6中模态与非模态对话框区别
开发语言·c++·qt
岁忧5 小时前
(LeetCode 面试经典 150 题 ) 11. 盛最多水的容器 (贪心+双指针)
java·c++·算法·leetcode·面试·go
蜉蝣之翼❉10 小时前
CRT 不同会导致 fopen 地址不同
c++·mfc
aramae10 小时前
C++ -- STL -- vector
开发语言·c++·笔记·后端·visual studio
lixzest11 小时前
C++ Lambda 表达式详解
服务器·开发语言·c++·算法
_Chipen11 小时前
C++基础问题
开发语言·c++
灿烂阳光g12 小时前
OpenGL 2. 着色器
c++·opengl
AA陈超13 小时前
虚幻引擎UE5专用服务器游戏开发-20 添加基础能力类与连招能力
c++·游戏·ue5·游戏引擎·虚幻
mit6.82413 小时前
[Meetily后端框架] AI摘要结构化 | `SummaryResponse`模型 | Pydantic库 | vs marshmallow库
c++·人工智能·后端
R-G-B13 小时前
【02】MFC入门到精通——MFC 手动添加创建新的对话框模板
c++·mfc·mfc 手动添加创建新的对话框