我有一双 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/