Convert tuple to variadic arguments

I’m not sure what I want to achieve is possible, but I need my templated function (my_func) to be able to take both variable arguments as well as a tuple argument. Clearer description is in the code comments.

struct A{
    static void call_func(int a, std::string b) {
        std::cout<<"int is: "<<a<<", string is: "<<b<<"\n";

struct B{
    static void call_func(std::string a, std::string b) {
        std::cout<<"string is: "<<a<<", string is: "<<b<<"\n";

struct C{
    static void call_func(int a) {
        std::cout<<"int is: "<<a<<"\n";

template<typename T, typename... Args> 
void my_func(Args... args) {

int main() {

    my_func<A>(3, "ant");    //int is: 3, string is: ant
    my_func<B>("bat", "bee");  //string is: bat, string is: bee
    my_func<C>(5);  //int is: 5

    //Is there a way to call(or modify) my_func such that it also accepts a tuple
    std::tuple<int, std::string> a_params{3,"tuple-ant"};
    std::tuple<std::string, std::string> b_params{"tuple-bat","tuple-bee"};
    std::tuple<int> c_params{7};
    // my_func<A>(a_params); 
    // my_func<B>(b_params);
    // my_func<C>(c_params);   

>Solution :

Are you sure it’s a good idea that both functions are called my_func()? To avoid confusion, what about my_func_tuple(), or something similar, for the second one?

Anyway… it seems to me that you’re looking for std::apply(); with it the tuple version can be written as

template <typename T, typename ... Args>
void my_func(std::tuple<Args...> tpl) {
  std::apply(T::call_func, tpl);

Take in count that std::apply() is available starting from C++17; before C++17 (C++11 and C++14) you have to simulate it using a integer sequence; see the Mooing Duck answer for a good example.

Leave a Reply