c++ variadic template pack expansion by groups of

2019-07-28 19:16发布

(Somehow related to this previous question)

I want to evaluate the parameters of a template function by groups of N parameters. Something like this:

template <size_t N, typename ... Ts>
void evaluate(Ts const & ... fn)
{
    for( int i=0; i<2; i++ )
        runH<N>::func(i, fn...);
}
int main()
{
    run<3>( // N = 2
        [](int i){ cout << "lambda func 1: " << std::to_string( i ) << endl; },
        [](int i){ cout << "lambda func 2: " << std::to_string( i ) << endl; },
        [](int i){ cout << "lambda func 3: " << std::to_string( i ) << endl; },
        [](int i){ cout << "lambda func 4: " << std::to_string( i ) << endl; },
        [](int i){ cout << "lambda func 5: " << std::to_string( i ) << endl; }
    );
}

should output:

lambda func 1: 0
lambda func 2: 0
lambda func 1: 1
lambda func 2: 1
lambda func 3: 0
lambda func 4: 0
lambda func 3: 1
lambda func 4: 1
lambda func 5: 0
lambda func 5: 1

Remainder must be handled properly. So far I managed to evaluate just the first group of N parameters with this:

template <std::size_t N>
struct runH
{
    template <typename T0, typename ... Ts>
    static void func (const int i, T0 const & f0, Ts const & ... fn)
    {
        f0(i);
        runH<N-1U>::func(i, fn...);
    }
};

template <>
struct runH<0>
{
    template <typename ... Ts>
    static void func (const int i, Ts const & ... fn) { }
};

template <size_t N, typename ... Ts>
void evaluate(Ts const & ... fn)
{
    for( int i=0; i<2; i++ )
        runH<N>::func(i, fn...);
}

template <std::size_t N, typename ... Ts>
void run (Ts const & ... fn)
{
    using unused = int[];
    (void)unused { 0, (evaluate<N>(fn...), 0) };
}

Is there any way for the run function to keep expanding the parameters? I tried to add another ellipsis at the end but it doesn't compile.

1条回答
▲ chillily
2楼-- · 2019-07-28 19:30

I propose the definition of a runSkip struct

template <std::size_t N, std::size_t I>
struct runSkip
 {
   template <typename T0, typename ... Ts>
   static void func (T0 const &, Ts const & ... fn)
    { runSkip<N, I-1U>::func(fn...); }

   static void func ()
    { }
 };

template <std::size_t N>
struct runSkip<N, 0U>
 {
   template <typename ... Ts>
   static void func (Ts const & ... fn)
    { run<N>(fn...); }
 };

so run() become

template <std::size_t N, typename ... Ts>
void run (Ts const & ... fn)
 {
   using unused = int[];

   std::size_t j;

   for ( auto i = 0 ; i < 2 ; ++i )
    {
      j = 0U;

      (void)unused { 0, ((++j <= N ? ((void)fn(i), 0) : 0), 0)... };
    }

   runSkip<N, N>::func(fn...);
 }

The following is a full working example

#include <iostream>

template <std::size_t, typename ... Ts>
void run (Ts const & ...);

template <std::size_t N, std::size_t I>
struct runSkip
 {
   template <typename T0, typename ... Ts>
   static void func (T0 const &, Ts const & ... fn)
    { runSkip<N, I-1U>::func(fn...); }

   static void func ()
    { }
 };

template <std::size_t N>
struct runSkip<N, 0U>
 {
   template <typename ... Ts>
   static void func (Ts const & ... fn)
    { run<N>(fn...); }
 };

template <std::size_t N, typename ... Ts>
void run (Ts const & ... fn)
 {
   using unused = int[];

   std::size_t j;

   for ( auto i = 0 ; i < 2 ; ++i )
    {
      j = 0U;

      (void)unused { 0, ((++j <= N ? ((void)fn(i), 0) : 0), 0)... };
    }

   runSkip<N, N>::func(fn...);
 }

int main()
 {
   run<2>( // N = 2
      [](int i){ std::cout << "lambda func 1: " << i << std::endl; },
      [](int i){ std::cout << "lambda func 2: " << i << std::endl; },
      [](int i){ std::cout << "lambda func 3: " << i << std::endl; },
      [](int i){ std::cout << "lambda func 4: " << i << std::endl; },
      [](int i){ std::cout << "lambda func 5: " << i << std::endl; },
      [](int i){ std::cout << "lambda func 6: " << i << std::endl; }
      );
 }

P.s.: there is no need of std::to_string to send an integer to an output stream operator.

查看更多
登录 后发表回答