kpx1894
kpx1894

Reputation: 391

why parameter pack expansion is so limited?

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);
}

Upvotes: 3

Views: 353

Answers (1)

Pradhan
Pradhan

Reputation: 16757

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.

Upvotes: 3

Related Questions