3 Replies - 2776 Views - Last Post: 18 February 2012 - 08:53 PM

#1 Karel-Lodewijk   User is offline

  • D.I.C Addict
  • member icon

Reputation: 454
  • View blog
  • Posts: 864
  • Joined: 17-March 11

[C++11] Expanding variadic parameter pack

Posted 13 February 2012 - 05:57 AM

Description: expand<type>(params...); will return a std::array<T, type> of the parameters. If type is not supplied as a template parameter it will deduct the type from the first parameter.Variadic functions can let a function take any number of parameters, but you can not iterate over it, even if all parameters are of the same type, forcing you to write recursive code. There are snippets on the Internet but they vary from ugly to very ugly. Here is my attempt at keeping the ugliness to a minimum.
#include <iostream>
#include <array>

//base case of recursion that will fill the array
template <class Iterator>
void expand_array(Iterator it) {
    return;
}

//recursive functions that will fill the array
template <class T, class Iterator, class... Rest>
void expand_array(Iterator it, T head, Rest... rest) {
    *it = head;
    expand_array(++it, rest...);
}

//expand with optional type parameter
template <class T, class... Rest>
std::array<T, sizeof...(Rest)+1> expand(T head, Rest... rest) {
    //we take the head as a seperate parameter because we can
    //deduct the type from it that way if it is not supplied.
    std::array<T, sizeof...(rest)+1> c;
    expand_array(c.begin(), head, rest...);
    return c;
}

//expand with no parameters because we don't want this to break
template <class T>
std::array<T,0> expand() {
    return std::array<T,0>();
}

//Example of an 'iterative' variadic sum function
template <class... Params>
int sum(Params... params) {
    auto l = expand<int>(params...);
    int sum = 0;
    for (auto it = l.begin(); it != l.end(); ++it)
        sum += *it;
    return sum;
}

int main() {
    std::cout << sum(1,2,3,4) << std::endl;
}


Is This A Good Question/Topic? 0
  • +

Replies To: [C++11] Expanding variadic parameter pack

#2 Karel-Lodewijk   User is offline

  • D.I.C Addict
  • member icon

Reputation: 454
  • View blog
  • Posts: 864
  • Joined: 17-March 11

Re: [C++11] Expanding variadic parameter pack

Posted 13 February 2012 - 05:59 AM

Note that expand() (no template parameter and no parameters) is still invalid because there is just no way to deduct which type of empty container it should build.
Was This Post Helpful? 0
  • +
  • -

#3 Karel-Lodewijk   User is offline

  • D.I.C Addict
  • member icon

Reputation: 454
  • View blog
  • Posts: 864
  • Joined: 17-March 11

Re: [C++11] Expanding variadic parameter pack

Posted 13 February 2012 - 06:08 AM

Okay, do I feel foolish now, basically you should do std::array l = {params...}; How did I not think of this/find this.
Was This Post Helpful? 0
  • +
  • -

#4 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1747
  • View blog
  • Posts: 5,898
  • Joined: 03-August 09

Re: [C++11] Expanding variadic parameter pack

Posted 18 February 2012 - 08:53 PM

lol, cool stuff regardless. it still shows basic varadic templates really well!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1