我如何 "expand" 一个编译时 std::array 成一个参数包?

How do I "expand" a compile-time std::array into a parameter pack?

我想使用部分模板特化来 'break down' 一个数组(在编译时创建)到一个由它的值组成的参数包(与我在我的定义中定义的其他结构接口)代码)。以下(我的第一次尝试)未编译

#include <array>

template <typename T, auto k> struct K;
template <typename T, std::size_t... A> struct K<T, std::array<std::size_t, sizeof...(A)>{A...}> {};

因为模板参数 std::array<long unsigned int, sizeof... (A)>{A ...} must not involve template parameters。据我了解,如果非类型参数非常依赖于模板参数,则不可能在部分模板特化中提供非类型参数。因此,我试图通过将值包含在一个类型中来解决这个问题:

#include <array>

template <auto f> struct any_type;

template <typename T, typename array_wrapper> struct FromArr;
template <typename T, std::size_t... A>
struct FromArr<T, any_type<std::array<std::size_t, sizeof...(A)>{A...}>> {};

int main() {
  FromArr<int, any_type<std::array<std::size_t, 2>{1, 2}>> d;
  (void) d;
}

但是,在这里,部分模板专业化在我尝试使用时失败了;上面的定义与我使用它的方式不符,我不确定为什么。它因以下错误而失败:

file.cc: In function ‘int main()’:
file.cc:10:55: error: aggregate ‘FromArr<int, Any<std::array<long unsigned int, 2>{std::__array_traits<long unsigned int, 2>::_Type{1, 2}}> > d’ has incomplete type and cannot be defined
  10  |   FromArr<int, Any<std::array<std::size_t, 2>{1, 2}>> d;

是否可以解决此问题/使用不同的方法将数组作为参数包进行接口?

使用的编译器

我使用g++-10.0 (GCC) 10.0.1 20200124 (experimental)并通过g++ -std=c++2a file.cc编译,因为我使用非类型模板参数,所以需要c++2a。

编辑:

描述如何处理数组

在我的真实代码中,我有一个结构,它依赖于参数包 (1)。如果我能够使用数组 (2)(我在另一段代码中将其作为非类型模板参数)与该结构进行交互,那就太好了,如下面的代码所示。

template <int... s> struct toBeUsed;                               // (1)
template <std::size_t s, std::array<int, s> arr> struct Consumer { // (2)
    toBeUsed<arr> instance; // This is what I would like to do
}

我的尝试是编写一个如上所述的辅助结构 FromStruct,我可以用一个 array 实例化它,其中我有一个提供 toBeUsed 的 typedef FromStruct::type使用正确的参数,类似于 this example,这就是我想在这里用 std::tuple 组成的类型做的事情。

Link到例子

here I link 简化用法示例(第二个代码块)。

C++20 方法

参见 or, for possibly instructive but more verbose (and less useful) approach, revision 2 of this answer

C++17 方法

(这个答案最初包含一个使用次要 C++20 特性的方法(没有任何捕获的 lambda 可能是默认构造的),但受原始答案的启发,OP 提供了一个更简洁的方法C++20 方法利用了 constexpr std::array 属于可以在 C++20 中作为非类型模板参数传递的文字类型 class 的事实(对其 ::value_type) 进行了限制,结合对用于将数组解压缩到参数包中的索引序列使用部分专业化。但是,这个原始答案使用了一种将 std::array 包装成constexpr lambda (>=C++17) 作为 constexpr(特定的)std::array 创建者而不是实际的 constexpr std::array。有关详细信息这种方法,see revision 2 of this answer)

遵循 OP 的巧妙方法,下面是针对 C++17 的改编,使用非类型左值引用模板参数在编译时提供对数组的引用以构造目标数组。

#include <array>
#include <cstdlib>
#include <tuple>
#include <type_traits>
#include <utility>

// Parameter pack structure (concrete target for generator below).
template <typename StructuralType, StructuralType... s>
struct ConsumerStruct
{
    // Use tuple equality testing for testing correctness.
    constexpr auto operator()() const { return std::tuple{s...}; }
};

// Generator: FROM std::array TO Consumer.
template <const auto& arr,
          template <typename T, T...> typename Consumer,
          typename Indices = std::make_index_sequence<arr.size()> >
struct Generator;

template <const auto& arr,
          template <typename T, T...> typename Consumer,
          std::size_t... I>
struct Generator<arr, Consumer, std::index_sequence<I...> >
{
    using type =
        Consumer<typename std::remove_cv<typename std::remove_reference<
                     decltype(arr)>::type>::type::value_type,
                 arr[I]...>;
};

// Helper.
template <const auto& arr, template <typename T, T...> typename Consumer>
using Generator_t = typename Generator<arr, Consumer>::type;

// Example usage.
int main()
{
    // As we want to use the address of the constexpr std::array at compile
    // time, it needs to have static storage duration.
    static constexpr std::array<int, 3> arr{{1, 5, 42}};
    constexpr Generator_t<arr, ConsumerStruct> cs;
    static_assert(cs() == std::tuple{1, 5, 42});
    return 0;
}

请注意,此方法对 std::array 实例施加了限制,因为它需要具有静态存储持续时间。如果想避免这种情况,可以使用生成数组的 constexpr lambda 作为替代方法。

受到@dfri 的回答的启发,我将她/他的解决方案转换为一个可以省略函数的版本,而是只使用一个使用部分模板特化的结构 std::integer_sequence 这可能也很有趣其他:

template <auto arr, template <typename X, X...> typename Consumer,
          typename IS = decltype(std::make_index_sequence<arr.size()>())> struct Generator;

template <auto arr, template <typename X, X...> typename Consumer, std::size_t... I>
struct Generator<arr, Consumer, std::index_sequence<I...>> {
  using type = Consumer<typename decltype(arr)::value_type, arr[I]...>;
};

完整的用法示例:

#include <array>

/// Structure which wants to consume the array via a parameter pack.
template <typename StructuralType, StructuralType... s> struct ConsumerStruct {
  constexpr auto operator()() const { return std::array{s...}; }
};

/// Solution
template <auto arr, template <typename X, X...> typename Consumer,
          typename IS = decltype(std::make_index_sequence<arr.size()>())> struct Generator;

template <auto arr, template <typename X, X...> typename Consumer, std::size_t... I>
struct Generator<arr, Consumer, std::index_sequence<I...>> {
  using type = Consumer<typename decltype(arr)::value_type, arr[I]...>;
};

/// Helper typename
template <auto arr, template <typename T, T...> typename Consumer>
using Generator_t = typename Generator<arr, Consumer>::type;

// Usage
int main() {
  constexpr auto tup = std::array<int, 3>{{1, 5, 42}};
  constexpr Generator_t<tup, ConsumerStruct> tt;
  static_assert(tt() == tup);
  return 0;
}