1 template
2
3 void Print(T value)
4
5 {
6
7 std::cout << value << std::endl;
8
9 }
10
11 template
12
13 void Print(Head head, Rail... rail)
14
15 {
16
17 std::cout << head << ", ";
18
19 Print(rail...);
20
21 }
22
23 int main(int argc, _TCHAR* argv[])
24
25 {
26
27 Print(1);
28
29 Print("hello", 1);
30
31 Print(1, "hello");
32
33 Print(1, "hello", 'H');
34
35 getchar();
36
37 return 0;
38
39 }
View Code
在上面的代码中,我们先定义了一个只有一个模板参数的函数模板,它简单地输出传入的参数的值。然后又定义了一个可变参数的函数模板,它输出第一个参数的 值,然后递归地调用自己。注意rail...这种写法,它表示将函数参数包分割成一个一个的参数,并传入Print中。这样,函数参数包中的第一个参数传 递给head,剩余的参数又重新构成一个函数参数包传递给rail。当递归调用到函数参数包中只有一个参数时,则会调用只有一个模板参数的Print函 数。
理解了可变模板参数的使用原理后,我们再来编写一个自己的Printf函数。
1 void MyPrint(const char * pszText)
2
3 {
4
5 ASSERT(pszText != nullptr);
6
7 std::cout << pszText;
8
9 }
10
11 template
12
13 void MyPrint(const char * pszText, T value, Args... args)
14
15 {
16
17 assert(pszText != nullptr);
18
19 while (*pszText)
20
21 {
22
23 if (*pszText == '%' && *++pszText != '%')
24
25 {
26
27 std::cout << value;
28
29 MyPrint(++pszText, args...);
30
31 return;
32
33 }
34
35 std::cout << *pszText++;
36
37 }
38
39 }
40
41 int _tmain(int argc, _TCHAR* argv[])
42
43 {
44
45 MyPrint(nullptr);
46
47 MyPrint("hello");
48
49 getchar();
50
51 return 0;
52
53 }
View Code上述代码实现的功能针对可变参数模板的应用,当然不会像printf函数那么健壮,MyPrint识别格式化参数的标志就是%+下一位字符,如果条件满足的话就从Args参数中获取参数包分割后的第一个参数
原文链接: https://www.cnblogs.com/swarmbees/p/5621592.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/235909
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!