将C++模板类的参数包隐藏到std::tuple

Modern C++引入了可变模板以及模板参数包,以下。ios

template<typename... Args>
class MultiArgs{};

能够使用std::tuple将参数包隐藏起来,以下。c++

template<typename... Args>
class MultiArgs{
using Tuple = std::tuple<Args...>;
};

那么,怎么取出某个参数类型呢?好比,第0个,以及最后一个参数。code

能够借助std::tuple_element来实现。代码以下。element

template<typename... Args>
class MultiArgs{
using Tuple = std::tuple<Args...>;

template<std::size_t N>
using Type = typename std::tuple_element<N,Tuple>::type;
};

同时,咱们能够利用sizeof...来获取参数包的个数。string

static constexpr auto Size = sizeof...(Args);

所以,若是须要取出第0个和最后一个参数,能够使用如下代码。io

template<typename... Args>
class MultiArgs{
using Tuple = std::tuple<Args...>;
static constexpr auto Size = sizeof...(Args);

template<std::size_t N>
using Type = typename std::tuple_element<N,Tuple>::type;

using FirstArg = Type<0>;
using LastArg = Type<Size-1>;
};

若是上面的例子只能取出第0个和最后有一个参数,那么若是用户想要取出某个参数呢?完整代码以下。ast

#include <iostream>
#include <tuple>
#include <string>

template<typename... Args>
class MultiArgs{
public: 
using Tuple = std::tuple<Args...>;
static constexpr auto Size = sizeof...(Args);

template<std::size_t N>
using Type = typename std::tuple_element<N,Tuple>::type;

using FirstArg = Type<0>;
using LastArg = Type<Size-1>;

MultiArgs(){}
~MultiArgs(){}

void PrintFirstArgType(){
std::cout<<"The first arg type:"<<typeid(FirstArg).name()<<std::endl;
}

void PrintLastArgType(){
std::cout<<"The last arg type:"<<typeid(LastArg).name()<<std::endl;
}

template<std::size_t N>
void PrintType(){
std::cout<<"The arg at index:"<<N<<" type:"<<typeid(Type<N>).name()<<std::endl;
}
};

int main(){
MultiArgs<int,double,float,std::string>::Type<3> my_string;
my_string = "hello world";
std::cout<<"my_string:"<<my_string<<std::endl;

MultiArgs<int,double,float,std::string> multi_args;
multi_args.PrintFirstArgType();
multi_args.PrintType<1>();
multi_args.PrintType<2>();
multi_args.PrintLastArgType();

return 0;
}
相关文章
相关标签/搜索