dimanche 24 novembre 2019

Variadic templates: unfold arguments in groups

I have a function that takes two arguments:

template <typename T1, typename T2>
void foo(T1 arg1, T2 arg2)
{ std::cout << arg1 << " + " << arg2 << '\n'; }

And a variadic one that forwards its arguments in pairs :

template <typename... Args>
void bar(Args&&... args) {
    static_assert(sizeof...(Args) % 2 == 0);

    ( foo( std::forward<Args>(args), std::forward<Args>(args) ), ... );
    // ^ Sends each argument twice, not in pairs
}

Is there a way to do that ?

Aucun commentaire:

Enregistrer un commentaire