获取参数包的前N个元素

时间:2016-10-18 15:45:14

标签: c++ c++14 variadic-templates

我必须遵循以下问题:

template< size_t... N_i >
class A
{
  // ...
};

template< size_t N, size_t... N_i >
A</* first N elements of N_i...*/> foo()
{
  A</* first N elements of N_i...*/> a;

  // ...

  return a;
}

int main()
{
  A<1,2> res = foo<2, 1,2,3,4>();

  return 0;
}

在这里,我希望foo具有返回类型A</* first N size_t of N_i...*/>,即class A,其具有参数包N_i的前N个元素作为模板参数。

有谁知道如何实施?

6 个答案:

答案 0 :(得分:15)

这是我想到的最简短的解决方案(两行用于别名) 它遵循一个基于OP发布的代码的最小工作示例:

#include<functional>
#include<cstddef>
#include<utility>
#include<tuple>

template<std::size_t... V>
class A {};

template<std::size_t... V, std::size_t... I>
constexpr auto func(std::index_sequence<I...>) {
    return A<std::get<I>(std::make_tuple(V...))...>{};
}

template<std::size_t N, std::size_t... V>
constexpr auto func() {
    return func<V...>(std::make_index_sequence<N>{});
}

template<std::size_t N, std::size_t... V>
using my_a = decltype(func<N, V...>());

int main() {
    A<1,2> res1 = func<2, 1, 2, 3, 4>();
    // Or even better...
    decltype(func<2, 1, 2, 3, 4>()) res2{};
    // Or even better...
    my_a<2, 1, 2, 3, 4> res3{};
}

答案 1 :(得分:8)

最直接的子问题是在列表领域:

template <class... Ts>
struct typelist {
    using type = typelist;
    static constexpr std::size_t size = sizeof...(Ts);
};

template <class T>
struct tag { using type = T; };

template <std::size_t N, class TL>
struct head_n {
    using type = ???;
};

现在,head_n只是简单递归的问题 - 从空列表开始,将一个元素从一个列表移动到另一个列表N次。

template <std::size_t N, class R, class TL>
struct head_n_impl;

// have at least one to pop from and need at least one more, so just 
// move it over
template <std::size_t N, class... Ts, class U, class... Us>
struct head_n_impl<N, typelist<Ts...>, typelist<U, Us...>>
: head_n_impl<N-1, typelist<Ts..., U>, typelist<Us...>>
{ };

// we have two base cases for 0 because we need to be more specialized
// than the previous case regardless of if we have any elements in the list
// left or not
template <class... Ts, class... Us>
struct head_n_impl<0, typelist<Ts...>, typelist<Us...>>
: tag<typelist<Ts...>>
{ };

template <class... Ts, class U, class... Us>
struct head_n_impl<0, typelist<Ts...>, typelist<U, Us...>>
: tag<typelist<Ts...>>
{ };

template <std::size_t N, class TL>
using head_n = typename head_n_impl<N, typelist<>, TL>::type;

从这个到你的具体问题,我把这作为练习留给读者。

另一种方法是通过连接。将typelist<Ts...>的每个元素转换为typelist<T>typelist<>,然后将它们连接在一起。 concat很简单:

template <class... Ts>
struct concat { };

template <class TL>
struct concat<TL>
: tag<TL>
{ };

template <class... As, class... Bs, class... Rest>
struct concat<typelist<As...>, typelist<Bs...>, Rest...>
: concat<typelist<As..., Bs...>, Rest...>
{ };

然后我们可以做到:

template <std::size_t N, class TL, class = std::make_index_sequence<TL::size>>
struct head_n;

template <std::size_t N, class... Ts, std::size_t... Is>
struct head_n<N, typelist<Ts...>, std::index_sequence<Is...>>
: concat<
        std::conditional_t<(Is < N), typelist<Ts>, typelist<>>...
        >
{ };

template <std::size_t N, class TL>
using head_n_t = typename head_n<N, TL>::type;

后一种方法的优点是concat可以在给定适当operator+的折叠表达式中在C ++ 17中替换:

template <class... As, class... Bs>
constexpr typelist<As..., Bs...> operator+(typelist<As...>, typelist<Bs...> ) {
    return {};
}

允许:

template <std::size_t N, class... Ts, std::size_t... Is>
struct head_n<N, typelist<Ts...>, std::index_sequence<Is...>>
{
    using type = decltype(
        (std::conditional_t<(Is < N), typelist<Ts>, typelist<>>{} + ... + typelist<>{})
        );
};        

答案 2 :(得分:7)

这是@skypjack's answer的一个小变化,可以避免使用元组:

template <size_t... N_i,size_t... M_i>
auto foo2(std::index_sequence<M_i...>)
{
    constexpr size_t values[] = {N_i...};
    return A<values[M_i]...>();
}

template <size_t N,size_t... N_i>
auto foo()
{
    return foo2<N_i...>(std::make_index_sequence<N>());
}

答案 3 :(得分:4)

Boost.Hana

非常简单
namespace hana = boost::hana;

template<size_t... vals>
auto make_a(hana::tuple<hana::integral_constant<size_t, vals>...>)
{
    return A<vals...>{};
}

template<size_t N, size_t... vals>
auto foo(){
    constexpr auto front = hana::take_front(
        hana::tuple_c<size_t, vals...>,
        hana::integral_c<size_t,N>
    );
    return detail::make_a(front);
}

live demo

答案 4 :(得分:3)

您还可以使用variadic通用lambda表达式和可重用的辅助结构来执行编译时迭代:

#include <utility>
#include <tuple>

template <std::size_t N, class = std::make_index_sequence<N>>
struct iterate;

template <std::size_t N, std::size_t... Is>
struct iterate<N, std::index_sequence<Is...>> {
   template <class Lambda>
   auto operator()(Lambda lambda) {
      return lambda(std::integral_constant<std::size_t, Is>{}...);
   }
};

template <size_t... Is>
struct A { };

template <size_t N, size_t... Is>
auto foo() {
   return iterate<N>{}([](auto... ps){
      using type = std::tuple<std::integral_constant<std::size_t, Is>...>;
      return A<std::tuple_element_t<ps, type>{}...>{};
   });
}

int main() {
   decltype(foo<3, 1, 2, 3, 4>()) a; // == A<1, 2, 3> a;
}

答案 5 :(得分:2)

不幸的是,这种方法需要定义其他Helper类型

template< size_t... N_i >
class A
{
};

template <size_t... N_i>
struct Helper;

template <size_t... N_i>
struct Helper<0, N_i...>
{
    typedef A<> type;
};

template <size_t N0, size_t... N_i>
struct Helper<1, N0, N_i...>
{
    typedef A<N0> type;
};

template <size_t N0, size_t N1, size_t... N_i>
struct Helper<2, N0, N1, N_i...>
{
    typedef A<N0, N1> type;
};

template< size_t N, size_t... N_i >
typename Helper<N, N_i...>::type foo()
{
  typename Helper<N, N_i...>::type a;
  return a;
}