Given this type:
template<typename ...As>
struct Base {};
I need to implement a function
template<int i, typename ...As>
constexpr auto Tail() {
static_assert(i < sizeof...(As), "index out of range");
return ??;
}
which returns an instance of B
using the tail of type parameter list from index i
.
For example,
Tail<0, int, float, double>() -> Base<int, float, double>
Tail<1, int, float, double>() -> Base<float, double>
Tail<2, int, float, double>() -> Base<double>
Tail<3, int, float, double>() -> fails with static assert
I know how to get the type at index i
:
template <int64_t i, typename T, typename... Ts>
struct type_at
{
static_assert(i < sizeof...(Ts) + 1, "index out of range");
typedef typename type_at<i - 1, Ts...>::type type;
};
template <typename T, typename... Ts> struct type_at<0, T, Ts...> {
typedef T type;
};
but I can't get a working version for the entire problem.
If you don't mind using C++17. Even static_assert
is not needed:
template<unsigned i, typename T, typename ...Ts>
constexpr auto Tail() {
if constexpr (i == 0)
return Base<T, Ts...>();
else
return Tail<i - 1, Ts...>();
}
int main(){
Base<int, double, int> a = Tail<0, int, double, int>();
Base<double, int> b = Tail<1, int, double, int>();
Base<int> c = Tail<2, int, double, int>();
auto d = Tail<3, int, double, int>();
}
Btw, changed int
to unsigned
to avoid possibility of (nearly) infinite recursion for negative numbers.
For Tail
(why is it a function?), the approach can be almost the same as that of type_at
. The only thing you have to change is the base case of the recursion:
template <typename ... Ts>
struct types_from<0, Ts...> {
using type = Base<Ts...>; // we don't like nasty typedef syntax
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With