不定参数模板类
作者:互联网
#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; } 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。 所以在调用的时候,不能直接通过对象调用,只能通过模板关键字调用 } 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; }
标签:std,Parameters,void,参数,template,displayParam,Type,模板,不定 来源: https://www.cnblogs.com/yuguangyuan/p/16475000.html