存储可变模板的模板参数数

Storing the number of template arguments of a variadic template

本文关键字:参数 数数 存储      更新时间:2023-10-16

在以下示例中,

template <size_t... Entries>
struct StaticArray
{
  enum {N = sizeof...(Entries)};
  size_t array[N] = {Entries...};
};

enum中存储条目数对我来说更像是一种黑客攻击

这真的是教科书中存储条目数量的方法吗?还是有更干净的方法?

我更喜欢static constexpr成员:

template <size_t... Entries>
struct StaticArray
{
    static constexpr size_t N = sizeof...(Entries);
    size_t array[N] = {Entries...};
};

可能与一堆其他constexpr成员函数(size()begin()end()data()等)