glades
glades

Reputation: 4888

Why does pack expansion allow to constrain template-dependent types but not regular types?

Compare the invocation of foo() and bar() with an arbitrary amount of arguments. I can call bar() and constrain the parameter type to B but just in the case I equip B with a parameter pack. If I don't (like in A), gcc doesn't allow me to specify the exact type of the parameters passed and I would have to use a generic type and constrain it with std::enable_if or static_asserts.

Just out of curiosity, why isn't this possible? It would be much clearer and consiser than using enable_if's or asserts.

#include <vector>
#include <iostream>

struct A { };

template <bool F>
struct B { };

void foo(A... nr)
{
    // doesn't work
}

template <bool... F>
void bar(B<F>... nr)
{
    // does work
}

int main()
{
    A a1, a2, a3;
    B<true> b1, b2, b3;
    foo(a1, a2, a3);
    bar(b1, b2, b3);
}

Upvotes: 0

Views: 20

Answers (1)

Caleth
Caleth

Reputation: 63162

Pack expansion is a feature of templates, not of functions. That's (currently) how the language is specified.

There a various ways of specifying "an arbitrary number of As", e.g.

foo(std::initialiser_list<A> as);   
foo(std::vector<A> as);

With a slight modification any of those could be called from your main:

foo({a1, a2, a3});

Upvotes: 1

Related Questions