1

(不知怎的,與此相關的以前questionC++通過任意大小

組可變參數模板包擴我想的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功能,不斷擴大的參數?我試圖在最後添加另一個省略號,但它不能編譯。

回答

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

PS:沒有必要的std::to_string將整數發送到輸出strea米操作員。