zoukankan      html  css  js  c++  java
  • C++ 11 可变模板参数的两种展开方式

    #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
  • 相关阅读:
    第32周二
    第32周一
    第31周日
    第31周六
    第31周五
    第31周四
    第31周三
    C++中this指针的使用方法.
    ArcPad 10 的安装部署
    UEditor用法
  • 原文地址:https://www.cnblogs.com/tangxin-blog/p/7466999.html
Copyright © 2011-2022 走看看