C++11新特性之不定参模板

模板函数

示例代码1

#include <iostream>

using namespace std;

/* 递归调用的终止条件 */
void test_variadic_templete_func()
{
   
   
    cout<<"The end!!!"<<endl;
}


/* 不定参函数模板的使用 采用的是递归的方式 */
template<typename T,typename ... Types>
void  test_variadic_templete_func(const T &frist,const Types&... args)
{
   
   
    cout<<frist<<endl;
    cout<<"variadic template function sizeof Usage:"<<sizeof...(args)<<endl;
    test_variadic_templete_func(args...);
}


int main(int argc, char const *argv[])
{
   
   
    test_variadic_templete_func("hhaah",112,333,222,999,000);
    return 0;
}

采用递归的方式拆解函数模板中的不定参。一定要有一个特化模板去做终止条件

程序运行截图

在这里插入图片描述

示例代码2

#include <iostream>
namespace max_test
{
   
   
    template<typename T>
    T max( T n)
    {
   
   
        return n;
    }
    template<typename T, typename ... Args>
    T max(T n,Args ... args)
    {
   
   
        return std::max(n,max(args...));
    }
} // namespace max_test



int main(int argc, char const *argv[])
{
   
   
    std::cout<< "The max is "<<max_test::max(1,2,3,4,5,6,7,22)<<std::endl;
    return 0;
}

程序运行截

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值