2

假设,我有一些序列,例如斐波那契数,定义为模板:

template <unsigned int N> struct Fibonacci { unsigned int value = /*...*/; };

我需要的是获得具有该序列的前 N ​​个元素的 constexpr 数组。我可以做到,使用可变参数模板:

template <unsigned int ... Numbers>
struct FibArray
{
    static constexpr array<unsigned int, sizeof...(Numbers)> value = { Fibonacci<Numbers>::value... };
};
// and then:
const auto fib_array = FibArray<1, 2, 3, 4, 5, 6, 7>::value;

是否有可能避免手动枚举索引,并获得具有多个所需值的相同数组?像这样的东西:

const array<unsigned, 7> fib_array = GetFirstNFibValues<7>::value;
4

1 回答 1

5

您可以通过生成索引来获得此行为:

template<unsigned...> struct indices{};

template<unsigned N, unsigned... Indices>
struct indices_gen : indices_gen<N-1, N-1, Indices...>{};

template<unsigned... Indices>
struct indices_gen<1, Indices...>{
  using type = indices<1, Indices...>;
};

#include <array>

template<unsigned N>
struct fibonacci{
  static constexpr unsigned value = N; // yes, lazyness on my part
};

template<class IPack>
struct first_n_fib_impl;

template<unsigned... Is>
struct first_n_fib_impl<indices<Is...>>
{
  using arr_type = std::array<unsigned, sizeof...(Is)>;
  static constexpr arr_type value = {{ fibonacci<Is>::value... }};
};

template<unsigned... Is>
constexpr std::array<unsigned, sizeof...(Is)> first_n_fib_impl<indices<Is...>>::value;

template<unsigned N>
struct first_n_fib
  : first_n_fib_impl<typename indices_gen<N>::type>
{
};

活生生的例子(证明[1..7]确实是生成的)。

于 2012-10-03T11:32:07.743 回答