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 7646217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:09:04+00:00 2026-05-31T10:09:04+00:00

I was looking at this SO question and I couldn’t understand how the answer

  • 0

I was looking at this SO question and I couldn’t understand how the answer worked. I will post a copy of code in one of the answers for reference:

template<int ...> struct seq {};

// How does this line work?
template<int N, int ...S> struct gens : gens<N-1, N-1, S...> {};

template<int ...S> struct gens<0, S...>{ typedef seq<S...> type; };

double foo(int x, float y, double z)
{
    return x + y + z;
}

template <typename ...Args>
struct save_it_for_later
{
  std::tuple<Args...> params;
  double (*func)(Args...);

  double delayed_dispatch()
  {
     return callFunc(typename gens<sizeof...(Args)>::type());
  }

  template<int ...S>
  double callFunc(seq<S...>)
  {
     return func(std::get<S>(params) ...);
  }
};

int main(void)
{
  std::tuple<int, float, double> t = std::make_tuple(1, 1.2, 5);
  save_it_for_later<int,float, double> saved = {t, foo};
  cout << saved.delayed_dispatch() << endl;
}

The part I don’t understand is this:

template<int N, int ...S> struct gens : gens<N-1, N-1, S...> {};

In the example at return callFunc(typename gens<sizeof...(Args)>::type()); I’m assuming that sizeof..(Args) would be 3.
So,

template<int N, int ...S> struct gens : gens<N-1, N-1, S...> {};

becomes

template<3, {}> struct gens : gens<3-1, 3-1, {}> {};

Is this correct, and if so, what happens from there?

  • 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-05-31T10:09:05+00:00Added an answer on May 31, 2026 at 10:09 am

    Let’s write down the recursion manually:

    gens<3> : gens<2, 2>
    gens<3> : gens<2, 2> : gens<1, 1, 2>
    gens<3> : gens<2, 2> : gens<1, 1, 2> : gens<0, 0, 1, 2>
    

    The recursion stops because of the partial specialization for 0:

    struct gens<0, S...>{ typedef seq<S...> type; }; 
    
    // first 0 consumed by the partial specialization
    // S = 0,1,2
    struct gens<0, 0, 1, 2> { 
        typedef seq<0, 1, 2> type;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've asked this question before but couldn't get the answer I was looking for
While looking up the answer to this question: Why is an out parameter not
I have been looking around for an answer to my question but couldn't find
This question stems from this question Code documentation how much is too much? One
I couldn't seem to find a similar question to this. I am currently looking
I googled this question and I couldn't find an answer. I have a page
This question has answer in other languages/platforms but I couldn't find a robust solution
I have scoured the internet looking for an answer to this question and can't
This is probably a beginner question, but I've already tried looking for an answer
I've been looking for this question and couldn't find any, sorry if it's duplicated.

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.