Thread: How to create a variadic function that are recursive with the given pseudo code in C+

  1. #1
    Registered User
    Join Date
    May 2020
    Posts
    1

    How to create a variadic function that are recursive with the given pseudo code in C+

    I am trying to make a recursive variadic template function that works in the following example. The example shows no practicality and is only used to help better understand what I'm asking. So, basically I have a function that takes an infinite amount of parameters of the same type and it recursively calls itself with all but the last parameters. Then, once it finally reaches two parameters the next unction serves as the termination of the function. I know the answer isn't trivial and I'm having trouble figuring out exactly what to do. Any help or direction would be appreciated! Thanks.

    Code:
    template <typename... Ts>
    void test(int& a, int& b, Ts&... ts){
        test(a, b, ... ); //all but the last parameter
    
    
        //last parameter argument is processed here
    
    
    }
    
    
    void test(int& a, int& b){
        //end of recursion
    }
    
    
    int main(int argc, char** argv){
    
    
        int a = 3;
        int b = 5;
        int c = 6;
        int d = 4;
    
    
        test(a, b, c, d);
    
    
        return 0;
    }


  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,748
    I am not very familiar with variadic function templates, and I'm afraid that I have never seen an example in which the parameter pack is not at the end. This means that you can only process the first (or first N) variadic arguments, and then unpack the rest, whereas you want to unpack all the variadic arguments other than the last argument. I gave it a try:
    Code:
    #include <iostream>
    
    void test(int& a, int& b)
    {
        std::cout << "end: " << a << ", " << b << std::endl;
    }
    
    template <typename T>
    void test(int& a, int& b, T& c)
    {
        test(a, b);
        std::cout << c << std::endl;
    }
    
    template <typename T, typename... Ts>
    void test(int& a, int& b, Ts&... ts, T& c)
    {
        test(a, b, ts...);
        std::cout << c << std::endl;
    }
    
    int main()
    {
        int a = 3;
        int b = 5;
        int c = 6;
        int d = 4;
    
        test(a, b, c, d);
    
        return 0;
    }
    but it failed. You may want to change your requirements so you can write this instead:
    Code:
    #include <iostream>
    
    void test(int& a, int& b)
    {
        std::cout << "end: " << a << ", " << b << std::endl;
    }
    
    template <typename T>
    void test(int& a, int& b, T& c)
    {
        test(a, b);
        std::cout << c << std::endl;
    }
    
    template <typename T, typename... Ts>
    void test(int& a, int& b, T& c, Ts&... ts)
    {
        test(a, b, ts...);
        std::cout << c << std::endl;
    }
    
    int main()
    {
        int a = 3;
        int b = 5;
        int c = 6;
        int d = 4;
    
        test(a, b, d, c);
    
        return 0;
    }
    (Notice that c and d are swapped in the call in main.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how create a Variadic Templates?
    By joaquim in forum C++ Programming
    Replies: 7
    Last Post: 10-04-2015, 09:58 AM
  2. how to write variadic template for variadic function?
    By Dave11 in forum C++ Programming
    Replies: 4
    Last Post: 04-02-2015, 03:16 AM
  3. Replies: 7
    Last Post: 10-08-2012, 12:54 AM
  4. goto or function pointer into this pseudo-code?
    By polslinux in forum C Programming
    Replies: 3
    Last Post: 07-06-2012, 08:23 AM
  5. Recursive Function in Pseudo Code
    By smitsky in forum Tech Board
    Replies: 3
    Last Post: 10-24-2004, 10:17 AM

Tags for this Thread