代码之家  ›  专栏  ›  技术社区  ›  Lingxi

操作可变函数模板的函数参数

  •  1
  • Lingxi  · 技术社区  · 10 年前

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

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

    逻辑上 结束() 可以在以下方面实现 开始() 明确地 end(x, y, ..., z) 等于 begin(x, y, ..., z + 1) 那么,有没有干净的转弯方式 x, y, ..., z 进入 x, y, ..., z + 1 使用 indices ,以便我能够实现 结束() 喜欢

    template <typename... Ts>
    Iterator end(Ts... indices) const {
      return begin(whatever to do with indices...);
    }
    
    1 回复  |  直到 10 年前
        1
  •  5
  •   Yakk - Adam Nevraumont    10 年前
    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,但相对容易实现部分。

    推荐文章