C++11 – Variadic template example

#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;
}
Advertisements
This entry was posted in C++, Programming. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s