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

  • Home
  • SEARCH
  • 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 6531915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:56:11+00:00 2026-05-25T09:56:11+00:00

Here is my first code in variadic templates : void print_f() { } template<typename

  • 0

Here is my first code in variadic templates :

void print_f()
{
}

template<typename T, typename ... ARG>
void print_f(const T& a, ARG... C)
{
    std::cout<<a;
    print_f(C...); // -> at this line I want to know
    std::cout<<std::endl;
}

when I make a recursive call to print_f(C...); , what actually happens? what I mean is
template parameter pack C gets unpacked and how is function argument deduction is done in variadic templates , how is matching done?

can anyone please explain some basics?

Edit: Usually in template class like

template <typename T> class x{
public:
T aa; // can declare variable of type T
};

but in variadic templates :

template<typename T, typename ... ARG>
void print_f(const T& a /* , ARG... C -> if remove this */ )
{
    std::cout<<a;
    ARG... C // and put it here , we can't do this,  why?
    print_f(C...); 
    std::cout<<std::endl;
}
  • 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-25T09:56:11+00:00Added an answer on May 25, 2026 at 9:56 am

    I think your first printf overload must also be a template, but I’m not 100% sure.

    Anyway, it works more or less like this:

    printf("APPLE", 10, '@', 9.0); 
    // this calls printf(const char*, int, char, double)
    // with T=const char*, and ARG={int, char, double}
        printf(const char* a, ARG ... C) {
        std::cout<<a; // this displays "APPLE"
        print_f(C...); 
        // this calls printf(int, char, double)
        // with T=int, and ARG={char, double}
            printf(int a, ARG ... C) {
            std::cout<<a; // this displays 10
            print_f(C...); 
            // this calls printf(char, double)
            // with T=char, and ARG={double}
                printf(char a, ARG ... C) {
                std::cout<<a; // this displays '@'
                print_f(C...); 
                // this calls printf(double)
                // with T=double, and ARG={}
                    printf(double a, ARG ... C) {
                    std::cout<<a; // this displays 9.0
                    print_f(C...); 
                    // this calls printf()
                        printf() {
                        }
                    std::cout<<std::endl;
                std::cout<<std::endl;
            std::cout<<std::endl;
        std::cout<<std::endl;
    

    It should also be noted that with your code, all the values appear on the same line, followed by lots of blank lines. I think you wanted the endl before the recursion.

    As per your edit: In your hypothetical function template<typename T, typename ... ARG>
    void print_f(const T& a)
    , you would be unable to call this function unless you specified ARG yourself, as it cannot be deduced from the function parameter types, since ARG is no longer in the parameters.

    Also, variadic templates cannot be instantiated because they are not a type, they are a grouping of types. If you really wanted to instantiate something, you could use a tuple: std::tuple<ARGS> C;, but all the objects would be default constructed, since you didn’t pass parameters to the function to construct from.

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

Sidebar

Related Questions

Here is the code: string str; cin>>str; cout<<first input:<<str<<endl; getline(cin, str); cout<<line input:<<str<<endl; The
This is the second step of a question explained here: EF 4.1 code-first: How
First off here is the code! <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Strict//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd>
here's the main code at first I thought is was the message box but
Here is the first part of my controller code: public class ControlMController : Controller
Let me first put the code snippets here. I am just using the ASP.NET
first post here, and probably an easy one. I've got the code from Processing's
First, here is the C# code and the disassembled IL: public class Program<T> {
First, to make my job explaining a bit easier, here's some of my code:
So, here's my question: Why won't the code in the first snippet work when

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.