I have some auxiliary code that performs vector reshuffling using compile-time indices. It is of upmost importance that the generated code is as efficient as possible. I am relying on parameter packs with fold expressions, and I was wondering what is the best practice in writing such code.
A practical example: let there be a function insert
which inserts the elements of container y
into container x
at positions Ii
where the positions are compile-time constants. The basic signature of this function would be something like this:
template<size_t... Ii, size_t Xsize, size_t Size>
constexpr container<Xsize> insert(container<Xsize> x, container<Ysize> y);
And its invoked like this: insert<0, 2>(x, y)
. I see two obvious possibilities of implementing this.
First: using an auxiliary index variable to iterate over y
:
template<size_t... Ii, size_t Xsize, size_t Size>
constexpr container<Xsize> insert(container<Xsize> x, container<Ysize> y) {
int i = 0;
((x[Ii] = y[i++]), ...);
return x;
}
My problem with this solution is the variable i
: I have to rely on the compiler to optimise it out.
The second solution avoids any runtime dependencies, but it requires an auxiliary function, making the entire implementation rather ugly:
template<size_t... Ii, size_t... Yi, size_t Xsize, size_t Size>
constexpr container<Xsize> insert_(container<Xsize> x, container<Ysize> y, std::index_sequence<Yi...>) {
((x[Ii] = y[Yi]), ...);
return x;
}
template<size_t... Ii, size_t Xsize, size_t Size>
constexpr container<Xsize> insert(container<Xsize> x, container<Ysize> y) {
return insert_<Ii...>(x,y, std::make_index_sequence<sizeof...(Ii)> {});
}
Is there a way to get this done avoiding both runtime variables and an auxiliary function?