c++ - 统一扩展两个参数包

标签 c++ templates

我正在使用来自另一个容器的值更新容器中的一些元素(在编译时选择)。现在我正在做类似的事情:

template<size_t... indices>
void update(value_t values, index_sequence<indices...>) {
  int i = 0; 
  ((data[indices] = values[i++], ...);
} 

问题:是否可以通过使用另一个编译时序列来避免使用变量 i,例如以下几行内容:

template<size_t... indices1, size_t... indices2>
void update_helper(value_t values, index_sequence<indices1...>, 
                                   index_sequence<indices2...>) {
  static_assert(sizeof...(indices1) == sizeof...(indices2), "");
  ((data[indices1] = values[indices2], ...);
} 

template<size_t... indices>
void update(value_t values, index_sequence<indices...> i) {
  update_helper(values, i, make_index_sequence<sizeof...(indices)>{});
} 

最佳答案

是的,这是可能的(只要它们的大小相同)。

template <size_t... indices1, size_t... indices2>
void update_helper(value_t values,
                   index_sequence<indices1...>, 
                   index_sequence<indices2...>)
{
    static_assert(sizeof...(indices1) == sizeof...(indices2), "");
    ((data[indices1] = values[indices2], ...);
}

有效。

关于c++ - 统一扩展两个参数包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54128524/

相关文章:

c++ - 智能感知 :expected a declaration in C++

c++ - 初始化 PhysFS 时出现问题

c++ - 什么样的值是模板参数?我能(不能)用它们做什么?

c++ - CGAL 2.5D 三角测量附加顶点信息

c++ - C++ 中的不透明句柄

c++ - 如何防止静态库中的重复符号?

c++ - 通过模板特化继承

c++ - 带有参数的模板传递成员函数和返回值作为参数c++

c++ - 如何专门化模板模板的功能?

c++ - gcc/clang 上的模板错误,但 MSVC 上没有