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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:20:26+00:00 2026-06-11T01:20:26+00:00

This example uses a common variadic template and function. I want to print out

  • 0

This example uses a common variadic template and function. I want to print out the arguments passed to f:

#include <iostream>

template <typename T>
void print(T t) 
{
    std::cout << t << std::endl;
}

template <typename...T>
void f(T &&...args) 
{
    print(args...);
    f(args...);
}

int main() 
{
    f(2, 1, 4, 3, 5);
}

But I am getting the following errors:

Compilation finished with errors:<br>
source.cpp: In instantiation of '`void f(T ...)` [with `T = {int, int, int, int, int}`]':<br>
source.cpp:16:20: required from here <br>
source.cpp:10:4: error: no matching function for call to '`print(int&, int&, int&, int&, int&)`'<br>
source.cpp:10:4: note: candidate is:<br>
source.cpp:4:6: note: `template<class T> void print(T)`<br>
source.cpp:4:6: note: template argument deduction/substitution failed: 
source.cpp:10:4: note: candidate expects 1 argument, 5 provided

This is actually my first time using variadic functions and I do not exactly understand how to use them well.

I also do not get why this isn’t working and what I can do to help it.

  • 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-11T01:20:28+00:00Added an answer on June 11, 2026 at 1:20 am

    There you go. You had several mistakes in your code, you can see the comments between the lines below:

    #include <iostream>
    
    template <typename T>
    void print(T t) {
       std::cout << t << std::endl;
    }
    
    // Base case, no args
    void f() {}
    
    // Split the parameter pack.
    // We want the first argument, so we can print it.
    // And the rest so we can forward it to the next call to f
    template <typename T, typename...Ts>
    void f(T &&first, Ts&&... rest) {
        // print it
        print(std::forward<T>(first));
        // Forward the rest.
        f(std::forward<Ts>(rest)...);
    }
    
    int main() {
        f(2, 1, 4, 3, 5);
    }
    

    Note that using rvalue refs here makes no sense. You’re not storing the parameters anywhere, so simply passing them by const reference should do it. That way you’d also avoid using std::forward just to keep the (useless) perfect forwarding.

    Therefore, you could rewrite f as follows:

    template <typename T, typename...Ts>
    void f(const T &first, const Ts&... rest) {
        print(first);
        f(rest...);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This example uses a StringWriter to hold the serialized data, then calling ToString() gives
Trying to make this jQuery filter that uses .find case-insensitive. For example, when the
In this fiddle http://jsfiddle.net/dAHqe/2/ I've created examples for the 2 main uses (that I've
This example is simplified a bit, but in my ASP.NET web page in my
this example below works when hover event is trigered and when its not, its
This example is taken from w3schools . CREATE TABLE Persons ( P_Id int NOT
This example working great because here containment is body http://jsfiddle.net/roXon/hMmbK/2/ when i use container
This example shows what I am trying to do: http://jsfiddle.net/Dqjvy/ I would like it
in this example http://sorgalla.com/projects/jcarousel/examples/static_controls.html i need an active state on li so it should
In this example below: my $rs = $schema->resultset('CD')->search( { 'artist.name' => 'Bob Marley' 'liner_notes.notes'

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.