C++(20):to_array

C++20提供了to_array函数,可以方便的将数据转为array:

使用起来还是比较简单的,借用一下官方文档里的例子:

#include 
#include 
#include 

using namespace std;

int main()
{
    constexpr auto w1n = to_array({"Mary", "Patricia", "Linda", "Barbara", "Elizabeth", "Jennifer"});
    static_assert(is_same_v>);
    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>);
 
    auto a3 = to_array({0, 1, 3});
    static_assert(is_same_v>);
 
    auto a4 = to_array>({{3, 0.0f}, {4, 0.1f}, {4, 0.1e23f}});
    static_assert(a4.size() == 3);
 
    auto a5 = to_array({make_unique(3)});
    static_assert(a5.size() == 1);
    
    return 0;
}

你可能感兴趣的:(C/C++,c++)