C++20提供了to_array函数,可以方便的将数据转为array:
使用起来还是比较简单的,借用一下官方文档里的例子:
#include <array>
#include <memory>
#include <string_view>
using namespace std;
int main()
{
constexpr auto w1n = to_array<string_view>({"Mary", "Patricia", "Linda", "Barbara", "Elizabeth", "Jennifer"});
static_assert(is_same_v<decltype(w1n), const array<string_view, 6>>);
static_assert(w1n.size() == 6 and w1n[5] == "Jennifer");
auto a1 = to_array("foo");
static_assert(a1.size() == 4);
auto a2 = to_array({0, 2, 1, 3});
static_assert(is_same_v<decltype(a2), array<int, 4>>