我想创建一个忽略参数包上第一个类型的 N 个的元组,像这样
template<typename... Args>
class Foo
{
std::tuple</*Args<2>,Args<3>,Args<4> and so on*/> tup;
}
目前,我发现实现接近此目标的唯一解决方案是
template<typename... Args>
class Foo
{
std::vector<std::variant<std::monostate//work for empty parameter pack,Args...>> partargs;
}
但这让我未来的事情变得更加困难,所以我想知道是否有更好的解决方案?
使用类模板部分特化就足够了
#include <tuple>
template<std::size_t N, typename... Args>
struct ignore_first;
template<std::size_t N, typename First, typename... Args>
struct ignore_first<N, First, Args...> : ignore_first<N-1, Args...> { };
template<typename First, typename... Args>
struct ignore_first<0, First, Args...> {
using type = std::tuple<First, Args...>;
};
template<std::size_t N>
struct ignore_first<N> {
using type = std::tuple<>;
};
static_assert(std::is_same_v<
ignore_first<0, int, long, char>::type, std::tuple<int, long, char>>);
static_assert(std::is_same_v<
ignore_first<1, int, long, char>::type, std::tuple<long, char>>);
static_assert(std::is_same_v<
ignore_first<2, int, long, char>::type, std::tuple<char>>);
static_assert(std::is_same_v<
ignore_first<3, int, long, char>::type, std::tuple<>>);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句