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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:08:17+00:00 2026-05-16T20:08:17+00:00

I am a little confused about how can I read each argument from the

  • 0

I am a little confused about how can I read each argument from the tuple by using variadic templates.

Consider this function:

template<class...A> int func(A...args){
int size = sizeof...(A);
.... }

I call it from the main file like:

func(1,10,100,1000);

Now, I don’t know how I have to extend the body of func to be able to read each argument separately so that I can, for example, store the arguments in an array.

  • 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-16T20:08:17+00:00Added an answer on May 16, 2026 at 8:08 pm

    You have to provide overrides for the functions for consuming the first N (usually one) arguments.

    void foo() {
       // end condition argument pack is empty
    }
    
    template <class First, class... Rest> 
    void foo(First first, Rest... rest) {
        // Do something with first
        cout << first << endl; 
    
        foo(rest...); // Unpack the arguments for further treatment
    }
    

    When you unpack the variadic parameter it finds the next overload.

    Example:

    foo(42, true, 'a', "hello");
    // Calls foo with First = int, and Rest = { bool, char, char* }
    // foo(42, Rest = {true, 'a', "hello"}); // not the real syntax
    

    Then next level down we expand the previous Rest and get:

    foo(true, Rest = { 'a', "hello"}); // First = bool
    

    And so on until Rest contains no members in which case unpacking it calls foo() (the overload with no arguments).


    Storing the pack if different types

    If you want to store the entire argument pack you can use an std::tuple

    template <class... Pack>
    void store_pack(Pack... p) {
        std::tuple<Pack...> store( p... );
        // do something with store
    }
    

    However this seems less useful.

    Storing the pack if it’s homogeneous

    If all the values in the pack are the same type you can store them all like this:

    vector<int> reverse(int i) {
        vector<int> ret;
        ret.push_back(i);
        return ret;
    }
    
    template <class... R>
    vector<int> reverse(int i, R... r) {
        vector<int> ret = reverse(r...);
        ret.push_back(i);
        return ret; 
    }
    
    int main() {
        auto v = reverse(1, 2, 3, 4);
        for_each(v.cbegin(), v.cend(), 
            [](int i ) { 
                std::cout << i << std::endl; 
            }
        );
    }
    

    However this seems even less useful.

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

Sidebar

Related Questions

I'm a little confused about MongoDB's read locking. How many concurrent read operations can
I'm a little confused about the HTTP protocol, from what i know HTTP was
I'm a little confused about the scope of a ScriptDB datastore. I read that
I'm a little confused about egg files and installing them using easy_install, hope you
I'm a little bit confused about which method to use : 1- using Jquery
I'm a little confused about this paragraph on OO visibilty in PHP. was curious
i'm a little confused about these two qualifiers... With ARC instead of using weak
I am little confused about some concepts around Objective-C protocols and categories. Can protocols
I hope you forgive me if I'm a little confused about this. Jrails rewrites
I am a little confused about Accept-Encoding . I have Web Service which would

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.