【问题标题】:c++ variadic template pack expansion by groups of arbitrary sizeC ++ Variadic模板包由任意大小组扩展
【发布时间】:2017-11-29 14:27:02
【问题描述】:

(不知何故与之前的question有关)

我想通过N 参数组来评估模板函数的参数。像这样的:

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

应该输出:

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

余料必须妥善处理。到目前为止,我设法只评估了第一组 N 参数:

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

run 函数有没有办法不断扩展参数?我试图在最后添加另一个省略号,但它没有编译。

【问题讨论】:

    标签: c++ c++11 variadic-templates template-specialization variadic-functions


    【解决方案1】:

    我提出runSkip结构的定义

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

    所以run()变成了

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

    以下是一个完整的工作示例

    #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.:不需要std::to_string 将整数发送到输出流运算符。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-18
      • 1970-01-01
      • 1970-01-01
      • 2019-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多