是否可以定义模板参数包数组

时间:2018-09-01 00:42:07

标签: c++11 templates variadic-templates variadic-functions template-meta-programming

可能的重复项:Is it possible to "store" a template parameter pack without expanding it?

与上述问题类似,我想进一步探索并存储可变参数数组。

template<size_t N, typename... Args>
void foo(Args(&...args)[N]) {
  Args[N]... args2; // compilation error
}

这有可能实现吗?

最终目标是能够调用foo(),使其可变参数数组输入的副本发生变异,并对变异执行某些功能。因此,类似:

template<typename F, size_t N, typename... Args>
void applyAsDoubled(F f, Args(&...args)[N]) {
  Args[N]... args2;
  doublerMutation(args2...); // doubles each argument; external function, assume it cannot avoid having a side-effect on its parameters

  for (int i = 0; i < N; i++)
    f(args2[i]...);
}

将被呼叫且没有副作用:

int A[N] = { 1, 2, 3, 4, 5 };
int B[N] = { 2, 2, 2, 2, 2 };

applyAsDoubled(printAdded, A, B);

将打印6、8、10、12、14,其中AB没有突变。为了澄清起见,函数doublerMutation()是一个伪函数,表示一个会导致参数突变且无法重写的函数。

1 个答案:

答案 0 :(得分:1)

我为您提出了一个C ++ 14解决方案,该解决方案应适用于C ++ 11,并用std::index_sequencestd::make_index_sequence的替代品。

对于applyAsDoubled(),我建议简单地调用一个辅助函数,同时在整个数组数量上传递一个std::index_sequence

template <typename F, std::size_t N, typename ... As>
void applyAsDoubled (F f, As(&...as)[N])
 { applyAsDoubledH(f, std::make_index_sequence<sizeof...(As)>{}, as...); }

helper函数主要基于std::tuple(用于打包数组副本)和新的std::array(用于C型数组副本)

void applyAsDoubledH (F f, std::index_sequence<Is...> const & is,
                      Args(&...args)[N])
 {
   auto isn { std::make_index_sequence<N>{} };

   std::tuple<std::array<Args, N>...> tpl { getStdArray(args, isn)... };

   doublerMutation(tpl, is);

   for (auto ui { 0u } ; ui < N ; ++ui )
      f(std::get<Is>(tpl)[ui]...);
 }

观察对getStdArray()的呼叫

template <typename T, std::size_t N, std::size_t ... Is>
std::array<T, N> getStdArray (T(&a)[N], std::index_sequence<Is...> const &)
 { return { { a[Is]... } }; }

获取单打std::array形式的单打C样式数组。

doublerMutation()还使用了辅助功能

template <std::size_t I, std::size_t N, typename ... Args>
void doublerMutationH (std::tuple<std::array<Args, N>...> & tpl)
 {
   for ( auto ui { 0u } ; ui < N ; ++ui )
      std::get<I>(tpl)[ui] *= 2;
 }

template <std::size_t N, typename ... Args, std::size_t ... Is>
void doublerMutation (std::tuple<std::array<Args, N>...> & tpl,
                      std::index_sequence<Is...> const &)
 {
   using unused = int[];

   (void) unused { 0, (doublerMutationH<Is>(tpl), 0)... };
 }

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

#include <tuple>
#include <array>
#include <iostream>
#include <type_traits>

template <std::size_t I, std::size_t N, typename ... Args>
void doublerMutationH (std::tuple<std::array<Args, N>...> & tpl)
 {
   for ( auto ui { 0u } ; ui < N ; ++ui )
      std::get<I>(tpl)[ui] *= 2;
 }

template <std::size_t N, typename ... Args, std::size_t ... Is>
void doublerMutation (std::tuple<std::array<Args, N>...> & tpl,
                      std::index_sequence<Is...> const &)
 {
   using unused = int[];

   (void) unused { 0, (doublerMutationH<Is>(tpl), 0)... };
 }

template <typename T, std::size_t N, std::size_t ... Is>
std::array<T, N> getStdArray (T(&a)[N], std::index_sequence<Is...> const &)
 { return { { a[Is]... } }; }

template <typename F, std::size_t ... Is, std::size_t N, typename ... Args>
void applyAsDoubledH (F f, std::index_sequence<Is...> const & is,
                      Args(&...args)[N])
 {
   auto isn { std::make_index_sequence<N>{} };

   std::tuple<std::array<Args, N>...> tpl { getStdArray(args, isn)... };

   doublerMutation(tpl, is);

   for (auto ui { 0u } ; ui < N ; ++ui )
      f(std::get<Is>(tpl)[ui]...);
 }

template <typename F, std::size_t N, typename ... As>
void applyAsDoubled (F f, As(&...as)[N])
 { applyAsDoubledH(f, std::make_index_sequence<sizeof...(As)>{}, as...); }

int main ()
 {
   int  A[] = { 1, 2, 3, 4, 5 };
   long B[] = { 2, 2, 2, 2, 2 };

   auto printSum = [](auto const & ... as)
    {   
      using unused = int[];

      typename std::common_type<decltype(as)...>::type  sum {};

      (void)unused { 0, (sum += as, 0)... };

      std::cout << "the sum is " << sum << std::endl;
    };

   applyAsDoubled(printSum, A, B);
 }

如果还可以使用C ++ 17,使用模板折叠和逗号运算符的功能,则可以避免使用unused的数组,并且doublerMutation()可以简化如下

template <std::size_t N, typename ... Args, std::size_t ... Is>
void doublerMutation (std::tuple<std::array<Args, N>...> & tpl,
                      std::index_sequence<Is...> const &)
 { ( doublerMutationH<Is>(tpl), ... ); }

printSum() lambda测试函数如下

auto printSum = [](auto const & ... as)
 { std::cout << "the sum is " << (as + ...) << std::endl; };