C++11新特性:Variadic Templates

本文探讨了C++中的模板元编程技术,通过示例展示了如何使用模板参数包进行递归调用来处理不定数量的参数。示例包括自定义打印函数、模拟printf功能以及实现tuple容器。这些例子详细解释了如何处理参数包、递归终止条件以及参数个数的判断,深入理解C++模板元编程的关键概念。
摘要由CSDN通过智能技术生成

数量不定的模板参数,后续可添加任意类型参数,函数内部递归调用,逐一取出每个元素

注意点 :

1、... 就是一个pack(包),要注意三个 ... 的位置。①模板参数包 ②函数参数类型包 ③函数参数包

2、需要一个空的重载函数来应对最后一次函数递归参数为空的情况

3、 sizeof...(包参数名) 可知包内参数个数

示例1:

//空重载函数
void myPrint() {}

template <typename T, typename... Types>
void myPrint(const T& x, const Types&... args) {
	cout << x << endl;   //具体实现
	myPrint(args...);    //递归调用
}

void test() {
	myPrint('a', 10, 1.11);
}

结果:

示例2:自实现printf

//自实现pringf函数
void myPrint(const char* s) {
	while (*s) {
		if (*s == '%' && *(++s) != '%')
			throw runtime_error("invaild format");//传入参数不够
		cout << *s++;
	}
}

template<typename T, typename... vals>
void myPrint(const char* s, T value, vals... args) {
	while (*s) {
		if (*s == '%' && *(++s) != '%') {
			cout << value;
			myPrint(++s, args...);
			return;
		}
		cout << *s++;//正常运行不会到这,防止因错误传入过多参数陷入死循环
	}
	throw logic_error("extra arguments");//传入参数太多
}

void test() {
	myPrint("%d\nhh", 10);
}

示例3:递归继承,实现tuple容器

template<typename... Values> class myTuple; //声明

template<> class myTuple<> {}; //递归到最后的空类

template<typename Head, typename... Tail>
class myTuple<Head, Tail...> 
	: private myTuple<Tail...> {
public:
	typedef myTuple<Tail...> inherited;
	myTuple(){}
	myTuple(Head v, Tail... vTail)
		: m_Head(v), inherited(vTail...) {} //inherited(vTail...) 调用构造函数实现递归
	Head head() { return m_Head; }
	inherited& tail() { return *this; } //返回时转换类型到 myTuple<Tail...>
protected:
	Head m_Head;
};


void test() {
	myTuple<int, float, string> t(10, 10.10, "hello");
	cout << t.head() << endl;
	cout << t.tail().head() << endl;
	cout << t.tail().tail().head() << endl;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值