3

I was wondering, why parameter pack expansion is so limited in C++11 - is it just oversight in C++11 standard? Why is it not possible to do just bar(args)...;? It is possible to workaround that better than I did?

#include <iostream>

int bar(int i)
{
    std::cout << i << ' ';
    return 0;
}

template <typename T>
void boo(T t)
{
    bar(t);
}

template <typename T, typename... Args>
void boo(T t, Args&&... args)
{
    bar(t);
    boo(args...);
}

template <typename... Args>
void dummy(Args&&... args)
{
}

template <typename... Args>
void foo(Args&&... args)
{
    dummy(bar(args)...);

    std::cout << std::endl;

    int dummy1[sizeof...(Args)] = { (bar(args), 0)... };

    std::cout << std::endl;

    boo(args...);
}

int main()
{
    foo(1, 3, 5);
}
mc110
  • 2,825
  • 5
  • 20
  • 21
kpx1894
  • 381
  • 2
  • 8

1 Answers1

3

The reference mentions the different contexts in which parameter pack expansions are allowed. So, a slightly more succinct way of doing this is

auto a = {bar(args)...};

This won't work if the return type of bar is void.

Pradhan
  • 16,391
  • 3
  • 44
  • 59