I'm trying to write a function that returns a subset of a variadic argument pack under the form of an std::tuple
. The function should ideally have no runtime overhead (no unnecessary copies), and it should allow users to access lvalue
references and modify them.
Value types, lvalue
references and const lvalue
references should be maintained. Temporaries (rvalue
references), should be "converted" to value types to avoid creating invalid references (references to temporaries).
Example of desired results:
int lr = 5;
const int& clr = lr;
auto t = make_subpack_tuple(lr, clr, 5);
static_assert(is_same
<
decltype(t),
std::tuple<int&, const int&, int>
>{}, "");
// Ok, modifies lr:
std::get<0>(t) = 10;
// Compile-time error, intended:
// std::get<1>(t) = 20;
// Ok, 5 was moved into the tuple:
std::get<2>(t) = 30;
Example incomplete implementation:
template<typename... Ts>
auto make_subpack_tuple(Ts&&... xs)
{
return std::tuple
<
some_type_trait<decltype(xs)>...
>
(
std::forward<decltype(xs)>(xs)...
);
}
Does what I am trying to do make sense?
Is there a standard type-trait that can be used in place of some_type_trait
? Or should I implement my own solution?
static_for
that executes a callable object over heterogeneous values with an user-specified arity, and also allows the user to retrieve the current iteration number at compile time and break/continue (exit early) at compile time usingstatic_if
. Part of the implementation requires passing the firstN
arguments of the variadic argument pack to another inner function, and I was trying to generalize that by defining some variadic argument pack manipulation functions. Other thannth<I>
, I requiresubpack<I, J>
to fully generalize that behavior – Secretinstatic_for
iterates over the heterogeneous values in groups ofN
(whereN
is a template parameter specified by the user). The callable object that represents the body of thestatic_for
needs to have anoperator()
with the same arity. Also, I'm doing all of this for an open-source C++14 general purpose library (vrm_core), written for fun and learning purposes. Hope that answers your question :) – Secretindecltype(xs)
. The type packTs...
is exactly what you need. So trystd::tuple<Ts...>
– Flathead