Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7991915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:25:42+00:00 2026-06-04T13:25:42+00:00

Possible Duplicate: How do I expand a tuple into variadic template function's arguments? “unpacking”

  • 0

Possible Duplicate:
How do I expand a tuple into variadic template function's arguments?
“unpacking” a tuple to call a matching function pointer

In C++11 templates, is there a way to use a tuple as the individual args of a (possibly template) function?

Example:
Let’s say I have this function:

void foo(int a, int b)  
{  
}

And I have the tuple auto bar = std::make_tuple(1, 2).

Can I use that to call foo(1, 2) in a templaty way?

I don’t mean simply foo(std::get<0>(bar), std::get<1>(bar)) since I want to do this in a template that doesn’t know the number of args.

More complete example:

template<typename Func, typename... Args>  
void caller(Func func, Args... args)  
{  
    auto argtuple = std::make_tuple(args...);  
    do_stuff_with_tuple(argtuple);  
    func(insert_magic_here(argtuple));  // <-- this is the hard part  
}

I should note that I’d prefer to not create one template that works for one arg, another that works for two, etc…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T13:25:44+00:00Added an answer on June 4, 2026 at 1:25 pm

    Try something like this:

    // implementation details, users never invoke these directly
    namespace detail
    {
        template <typename F, typename Tuple, bool Done, int Total, int... N>
        struct call_impl
        {
            static void call(F f, Tuple && t)
            {
                call_impl<F, Tuple, Total == 1 + sizeof...(N), Total, N..., sizeof...(N)>::call(f, std::forward<Tuple>(t));
            }
        };
    
        template <typename F, typename Tuple, int Total, int... N>
        struct call_impl<F, Tuple, true, Total, N...>
        {
            static void call(F f, Tuple && t)
            {
                f(std::get<N>(std::forward<Tuple>(t))...);
            }
        };
    }
    
    // user invokes this
    template <typename F, typename Tuple>
    void call(F f, Tuple && t)
    {
        typedef typename std::decay<Tuple>::type ttype;
        detail::call_impl<F, Tuple, 0 == std::tuple_size<ttype>::value, std::tuple_size<ttype>::value>::call(f, std::forward<Tuple>(t));
    }
    

    Example:

    #include <cstdio>
    int main()
    {
        auto t = std::make_tuple("%d, %d, %d\n", 1,2,3);
        call(std::printf, t);
    }
    

    With some extra magic and using std::result_of, you can probably also make the entire thing return the correct return value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: trailing return type using decltype with a variadic template function I'm getting
Possible Duplicate: PHP get all arguments as array? Within a javascript function arguments always
Possible Duplicate: How to call a JavaScript function from PHP? I have a php
Possible Duplicate: Yield In VB.NET In C#, when writing a function that returns an
Possible Duplicate: Finding the Variable Name passed to a Function in C# In C#,
Possible Duplicate: How can I combine multiple rows into a comma-delimited list in Oracle?
Possible Duplicate: What is the difference between a function expression vs declaration in JavaScript?
Possible Duplicate: How to expand 'select' option width after the user wants to select
Possible Duplicate: In Vim: How do I delete a word and go into insert
Possible Duplicate: Why do I get a null pointer exception from TabWidget? I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.