c++ - 操作可变函数模板的函数参数

标签 c++ c++14 variadic-templates template-meta-programming

我有一双 begin()/end()方法声明如下:

template <typename... Ts>
Iterator begin(Ts... indices) const;

template <typename... Ts>
Iterator end(Ts... indices) const;

逻辑上,end()可以根据 begin() 来实现.具体来说,end(x, y, ..., z)相当于begin(x, y, ..., z + 1) .那么,有没有一种干净的方法来转换 x, y, ..., z进入x, y, ..., z + 1使用 indices ,这样我就可以实现 end()喜欢

template <typename... Ts>
Iterator end(Ts... indices) const {
  return begin(whatever to do with indices...);
}

最佳答案

template <std::size_t...Is,class... Ts>
Iterator end_impl(std::index_sequence<Is...>,Ts... indices) const{
  auto tup=std::tie(indices...);
  return begin(std::get<Is>(tup)..., std::get<sizeof...(Ts)-1>(tup)+1);
}
template <class... Ts>
Iterator end(Ts... indices) const{
  return end_impl(std::make_index_sequence<sizeof...(Ts)-1>{}, indices...);
}

只需添加一些完美的转发和隐私。

使用 C++14 但相对容易实现部分。

关于c++ - 操作可变函数模板的函数参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35051930/

相关文章:

C++ 3d数组

c++ - 可变递归模板 mem 有趣的特化

c++ - 如何对非类型参数包的元素执行算术运算?

c++ - 自定义 UniformRandomBitGenerator 编译失败

c++ - 调用基类构造函数而不命名其类

c++ - 获取 std::tuple 满足特征的第一个元素

c++ - 'addQueryItem' 中没有名为 'QUrl' 的成员 - 将 Qt 4.7 转换为 5.8

c++赋值运算符=用派生类重载

c++ - 为什么 std::string 没有标准定义的文字后缀?

c++11 - 作为可变参数模板参数的成员指针