可変長テンプレートとタプル

可変長テンプレートはタプルで受け取って,1つずつ処理できる?

#include <tuple>
#include <iostream>
template <typename ... T>
class Hoge{
	std::tuple<T...> args;
	public:
		Hoge(T... args):args(args...){}
		template <int i>
			typename std::tuple_element<i,decltype(args)>::type get(){
				return std::get<i>(args);
			}
};
int main(){
	Hoge<std::string,std::string,int> a("10","1.5",2);
	std::cout << a.get<0>() << std::endl;
	std::cout << a.get<1>() << std::endl;
	std::cout << a.get<2>() << std::endl;
	return 0;
}

と言う感じで, Hoge に可変長テンプレートで何個かの型と,その可変長引数を渡したあと,
クラス内で std::tuple に変換し,タプルとして扱う.
ところで,「…」という演算子は型名の前にある場合はパック演算子といい,
後ろにある場合はアンパック演算子というらしい.

コメントする