Современный C++ позволяет избежать явного создания вспомогательных структур, единственное предназначение которых состоит в том, чтобы упаковать вместе несколько параметров:
(Осторожно, C++14!)
#include <iostream>
#include <string>
#include <tuple>
#include <utility>
using namespace std;
void func(int one, double two) {
cout << one << ", " << two << endl;
}
void func(int one, double two, string three) {
cout << one << ", " << two << ", " << three << endl;
}
void func(int one, string three) {
cout << one << ", " << three << endl;
}
// Начало шаблонной магии
template <typename... Args, size_t... I>
void func_invoker(tuple<Args&...> args, index_sequence<I...>) {
func(get<I>(args)...);
}
template <typename... Args>
void func_helper(tuple<Args&...> args) {
func_invoker(args, index_sequence_for<Args...>{});
}
// Конец шаблонной магии
int main()
{
int one = 1;
double two = 2.0;
string three = "3";
func_helper(tie(one, two));
func_helper(tie(one, two, three));
func_helper(tie(one, three));
return 0;
}