C++11 - Variadic template example

less than 1 minute read

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <sstream>
#include <vector>
std::vector<std::string> to_string()
{
	return{};
}
template <typename ARG1, typename ... ARGS>
std::vector<std::string> to_string(ARG1 arg, ARGS... args)
{
	std::vector<std::string> result;
	std::stringstream str;
	str << arg;
	result.push_back(str.str());
	auto others = to_string(args...);
	result.insert(result.end(), others.begin(), others.end());
	return result;
}
int main()
{
	auto res = to_string(1, "test");
	for (auto& item : res) {
		std::cout << item << std::endl;
	}
    return 0;
}