首页 > 编程语言 > 详细

C++ 11 可变模板参数的两种展开方式

时间:2017-09-02 17:44:43      阅读:642      评论:0      收藏:0      [点我收藏+]
#include <iostream>
#include <string>
#include <stdint.h>

template<typename T>
T Sum(T t)
{
    std::cout << t << " = ";
    return t;
}
template<typename T, typename... Args>
T Sum(T head, Args...args)
{
    std::cout << head << " + ";
    return head + Sum(args...);
}


template<typename T>
void PrintArg(T t)
{
    std::cout << t << " ";
}
template<typename... Args>
void Expand(Args... args)
{
    std::initializer_list<int32_t> {(PrintArg(args), 0)...};
    std::cout << std::endl;
}

int32_t main()
{
    std::cout << Sum(1, 2, 3, 4) << std::endl;
    Expand(1, 2, 3, 4);
    std::cin.get();
    return 0;
}

效果:

1 + 2 + 3 + 4 = 10
1 2 3 4

 

C++ 11 可变模板参数的两种展开方式

原文:http://www.cnblogs.com/tangxin-blog/p/7466999.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!