request: tuple_slice
- Dominant language
- C++
- Stars
- 291
- Forks
- 97
- PR merge metrics
- No merged PRs in 30d
Description
I am manipulating function arguments in boost.histogram, which I pass around as `std::tuple`. I found that I need a `tuple_slice` function, something which strips off some elements from a tuple of values from the front and/or back. I use this implementation:
```
template
struct tuple_slice_impl {
template
static decltype(auto) apply(const T& t) {
return std::forward_as_tuple(std::get(t)...);
}
};
template
decltype(auto) tuple_slide(const T& t) {
using LN = mp11::mp_iota_c;
using OffsetAdder = mp11::mp_bind_front>;
using LN2 = mp11::mp_transform_q;
return mp11::mp_rename::apply(t);
}
```
Basically, I generate an integer sequence of indices of the elements that should remain in the slice and then use a parameter pack expansion of `std::get`s.
Usage:
```
auto tup1 = std::make_tuple(1, "foo", 3.0);
auto tup2 = tuple_slice<0, 2>(tup1); // == std::make_tuple(1, "foo");
auto tup3 = tuple_slice<1, 2>(tup1); // == std::make_tuple("foo", 3.0);
// and so on
```
I think this is a useful addition to Mp11. The implementation is not trivial and it seems like a common tuple manipulation.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.