可以将模板参数定义成能够接受任意多个模板参数的情况。这一类模板被称为变参模板
1、使用函数递归来解析参数
#include <iostream>
using namespace std;
template<typename T>
void print(const T& a){
cout << a << endl;
}
template<typename T, typename ...ARGS>
void print(const T& first, const ARGS& ...args){
cout << first << ",";
print(args...);
}
int main(){
print(1, 2, 3.34, 'c', "hello world");
return 0;
}
2、折叠表达式
从 C++17 开始,提供了一种可以用来计算参数包(可以有初始值)中所有参数运算结果的二
元运算符。
语法
( 形参包 运算符 ... ) | 一元右折叠 |
( ... 运算符 形参包 ) | 一元左折叠 |
( 形参包 运算符 ... 运算符 初值 ) | 二元右折叠 |
( 初值 运算符 ... 运算符 形参包 ) | 二元左折叠 |
运算符 | - | 下列 32 个二元运算符之一:+ - * / % ^ & | = < > << >> += -= *= /= %= ^= &= |= <<= >>= == != <= >= && || , .* ->*。在二元折叠中,两个 运算符 必须相同。 |
形参包 | - | 含有未展开的形参包且在顶层不含优先级低于转型(正式而言,是 转型表达式)的运算符的表达式 |
初值 | - | 不含未展开的形参包且在顶层不含优先级低于转型(正式而言,是 转型表达式)的运算符的表达式 |
注意开闭括号也是折叠表达式的一部分。
解释
折叠表达式的实例化按以下方式展开成表达式 e:
1) 一元右折叠 (E
运算符 ...)
成为 (E1
运算符 (
... 运算符 (EN-1
运算符 EN)))
2) 一元左折叠 (...
运算符 E)
成为 (((E1
运算符 E2)
运算符 ...)
运算符 EN)
3) 二元右折叠 (E
运算符 ...
运算符 I)
成为
(E1
运算符 (
... 运算符 (EN−1
运算符 (EN
运算符 I))))
4) 二元左折叠 (I
运算符 ...
运算符 E)
成为
((((I
运算符 E1)
运算符 E2)
运算符 ...)
运算符 EN)
(其中 N
是包展开中的元素数量)
将一元折叠用于长度为零的包展开时,只能使用下列运算符:
1) 逻辑与(&&)。空包的值是 true
2) 逻辑或(||)。空包的值是 false
3) 逗号运算符(,)。空包的值是 void()
3、示例
仿make_shared函数和thread类中变参的用法
#include <iostream>
using namespace std;
#include <list>
#include <memory>
#include <thread>
#include <functional>
template<typename T>
void print(const T& a)
{
// cout << "print(a)" << endl;
cout << a << endl;
}
template<typename T, typename ...ARGS>
void print(const T& first, const ARGS& ...args)
{
// cout << "print: " << sizeof...(ARGS) << endl;
cout << first << ", ";
print(args...);
}
template<typename... T>
void print2(const T& ... args){
((cout << args << ", "),...) << endl;
//((print(args)),...); //多次调用函数print(const T&)
}
class A{
public:
A(int a, char b, double c){
}
void test(int, double){
cout << "A::test(int,double)" << endl;
}
};
template<typename T, typename ...ARGS>
T* make_pointer(ARGS&&... args)
{
T* p = new T(std::forward<ARGS>(args)...);
return p;
}
template<typename FUNC, typename ...ARGS>
class MyThread{
public:
MyThread(FUNC func, ARGS... args){
func(args...);
}
};
template<typename RET, typename CLASS, typename ...ARGS>
class MyThread<RET(CLASS::*)(ARGS...), CLASS*, ARGS...>{
public:
MyThread(RET(CLASS::*func)(ARGS...) , CLASS* pThis, ARGS... args){
(pThis->*func)(args...);
}
};
int main(){
print(1, 2, 3.34, 'c', "hello world");
print2(1, 2, 3.34, 'c', "hello world");
A* p = make_pointer<A>(1, 'b', 1.2);
MyThread([](int a, double b){
cout << "[](int, double){}" << endl;
}, 1, 3.14);
A a(1, 2, 3);
MyThread(&A::test, &a, 1, 2.3);
return 0;
}