I have the following function which can take N arguments of different types, and forwards them to N functions templated on each individual type, in this manner (example with
Also for fun, this might be a bit too convoluted
#include
#include
template
void g(T&& t)
{
// This function gets called
}
template
void entry(void* p)
{
g(*(std::remove_reference_t*)p);
}
template
using table_t = std::array;
template
constexpr auto make_table()
{
return table_t{
entry...
};
}
template
void f_(const table_t&, int)
{
}
template
void f_(const table_t& table, int select, T&& t, Ts&&... ts)
{
if(select == N - sizeof...(Ts) - 1)
table[select]((void*)&t);
else
f_(table, select, std::forward(ts)...);
}
template
void f(int select, Ts&&... ts)
{
static constexpr auto table = make_table();
if(select < 0 || select >= int(sizeof...(Ts)))
throw "out of bounds";
f_(table, select, std::forward(ts)...);
}
Which rolls a vtable in f and dispatches accordingly to g.
Live