删除最后一种模板参数包

时间:2015-08-15 12:03:57

标签: c++ templates

我想使用模板参数包的类型作为不同模板的参数,但是切断了最后一个参数。

例如:

template <class... Ts> struct some_template;

template <class... Ts> struct foo
{
    using bar = some_template<magically_get_all_but_last(Ts)...>;
};

// I might be missing a few "typename"s, but you get the idea.
static_assert(std::is_same<foo<int, bool, std::string>::bar, some_template<int,bool> >::value); 

请注意,这与getting only the last parameter相反。

3 个答案:

答案 0 :(得分:5)

这是一个简单的方法,它使用std::tuple_element<I, Tuple>std::index_sequence<sizeof...(Ts) - 1来获取可变参数列表中除最后一个类型之外的所有类型。由于需要索引的参数包,所以有一个额外的间接,它被放入一个基础但可以在任何地方。

template <class T, class... Ts> struct foobase;
template <std::size_t... I, class... Ts>
struct foobase<std::index_sequence<I...>, Ts...> {
    using bar = some_template<typename std::tuple_element<I, std::tuple<Ts...>>::type...>;
};

template <class... Ts> struct foo
    : foobase<std::make_index_sequence<sizeof...(Ts) - 1>, Ts...>
{
};

答案 1 :(得分:3)

这是我使用C ++ 11的解决方案:

template <typename ...P>
struct dummy {};

template <template <typename ...> class Obj, typename T, typename ...P>
struct internal;

template <template <typename ...> class Obj, typename ...P1, typename T, typename ...P2>
struct internal<Obj, dummy<P1...>, T, P2...>
{
    using type = typename internal<Obj, dummy<P1..., T>, P2...>::type;
};

template <template <typename ...> class Obj, typename ...P1, typename T, typename L>
struct internal<Obj, dummy<P1...>, T, L>
{
    using type = Obj<P1..., T>;
};

template <template <typename ...> class T, typename ...P>
struct subst_all_but_last
{
    using type = typename internal<T, dummy<>, P...>::type;
};

像这样使用:

using bar = typename subst_all_but_last<some_template, Ts...>::type;

而不是

using bar = some_template<magically_get_all_but_last(Ts)...>;

答案 2 :(得分:0)

这似乎有效:

#include <tuple>
#include <utility>
#include <cstdint>

using std::size_t;
using std::tuple;

namespace 
{
    template <class Tuple, template <class...> class Template> struct apply_tuple_params_impl {};
    template <template <class...> class Template, class... Params>
    struct apply_tuple_params_impl<tuple<Params...>, Template>
    {
        typedef Template<Params...> type;
    };

    template <class Param, class Tuple> struct extend_tuple;
    template <class Param, class... Params> struct extend_tuple<Param, tuple<Params...>>
    {
        typedef tuple<Param, Params...> type;
    };

    template <class Param, class... Params> struct all_but_last_type_tuple_impl
    {
        typedef typename extend_tuple<Param, typename all_but_last_type_tuple_impl<Params...>::type>::type type;
    };

    template <class Param> struct all_but_last_type_tuple_impl<Param>
    {
        typedef tuple<> type;
    };
}

// pass the template parameters of a tuple to another template
template <class Tuple, template <class...> class Template>
using apply_tuple_params = typename apply_tuple_params_impl<Tuple, Template>::type;

// a tuple type with all the parameters except the last
template <class... Params>
using all_but_last_type_tuple = typename all_but_last_type_tuple_impl<Params...>::type;

并像这样使用它:

template <class... Ts> struct some_template;

template <class... Ts> struct foo
{
    typedef apply_tuple_params<all_but_last_type_tuple<Ts...>, some_template> bar;
};

static_assert(std::is_same<foo<int, bool, std::string>::bar, some_template<int,bool> >::value, "");