惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
博客园_首页
罗磊的独立博客
云风的 BLOG
云风的 BLOG
J
Java Code Geeks
Last Week in AI
Last Week in AI
D
DataBreaches.Net
Jina AI
Jina AI
博客园 - Franky
大猫的无限游戏
大猫的无限游戏
Apple Machine Learning Research
Apple Machine Learning Research
V
V2EX
D
Docker
MongoDB | Blog
MongoDB | Blog
B
Blog RSS Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
宝玉的分享
宝玉的分享
Engineering at Meta
Engineering at Meta
The Cloudflare Blog
博客园 - 三生石上(FineUI控件)
有赞技术团队
有赞技术团队
人人都是产品经理
人人都是产品经理
H
Help Net Security
T
The Blog of Author Tim Ferriss

博客园 - 于光远

泛型编程 分区理论知识整理 SPI协议及驱动开发框架 I2C协议及驱动开发框架 使用qemu 加载linux-6.18.1内核 linux 内核 策略模式 单例模式 Linux性能分析:从监控到优化的完整工具链 C++模板元编程实现序列化与反序列化 引用 bootchart linux 脚本打印时间 接口编程 独立于main运行的程序 类模板继承 std::ostream & operator<< Flash HIDL --HelloWorld
不定参数解析,结合tuple
于光远 · 2022-07-13 · via 博客园 - 于光远
#include <iostream>
#include <tuple>
using namespace std;
template<class... Args>
class student{
public:

    template<typename HEAD>
    void displayParam(const HEAD &head){
        
    }
    void displayParam(){
        
    }
    template<class HEAD, class... TAIL>
    void displayParam(HEAD && head,  TAIL &&... tail){
        cout<<head<<endl;
        displayParam(std::forward<TAIL>(tail)...);
    }

   // template<std::size_t I, class... AArgs>
   // class Parameters;

    // template<std::size_t I, class Head, class... PArgs>
    // class Parameters<I, Head, PArgs...> {
    // public:
    //     using Type = typename Parameters<I - 1, PArgs...>::Type;
    // };

    // template<class Head, class... PArgs>
    // class Parameters<0, Head, PArgs...> {
    // public:
    //     using Type = Head;
    // };

    // template<class Head>
    // class Parameters<0, Head> {
    // public:
    //     using Type = Head;
    // };

    // template<std::size_t I>
    // using ArgType = typename Parameters<I, Args...>::Type;

    //template<typename... Args>
    void init(Args&&...args){
        first = std::make_tuple(args...);
    }

    template<std::size_t I>
    //typename std::decay<ArgType<I>>::type 
    auto
    GetValue(){
        //typename std::decay<ArgType<I>>::type tmp = std::get<I>(first);
        //return tmp;
        return std::get<I>(first);
    }

    template<int I>
    int GetInt(){
        cout<<"+++++++"<<endl;
    }

    int NoTemplate(){
        cout<<" in NoTemplate() "<<endl;
        return 0;
    }

    std::tuple<Args...> first; 
};

//------------------------------------
template<class... Args,std::size_t... I>                            // default
void makeI(student<Args...> &A,std::index_sequence<I...>){

    A.displayParam(A.template GetValue<I>()...);
   // A.displayParam(A.GetValue<I>()...); //compile error
    cout<<"-------"<<endl;
    A.displayParam(1,2,3,4,5);

    A.template GetInt<1>();// 因为GetInt和GetValue 里面有 额外的template。 所以在调用的时候,不能直接通过对象调用,只能通过模板关键字调用

    A.NoTemplate();
}
template
<class... Args> void test(student<Args...> &A){ makeI(A,std::index_sequence_for<Args...>()); } //------------------------------------------- int main(){ student<int ,char,string> A; A.init(1,97,"sss"); test(A);// return 0; }