Namespaces
Variants

std:: to_array

From cppreference.net
헤더 파일에 정의됨 <array>
template < class T, std:: size_t N >
constexpr std:: array < std:: remove_cv_t < T > , N > to_array ( T ( & a ) [ N ] ) ;
(1) (C++20부터)
template < class T, std:: size_t N >
constexpr std:: array < std:: remove_cv_t < T > , N > to_array ( T ( && a ) [ N ] ) ;
(2) (C++20부터)

1차원 내장 배열 a 로부터 std::array 를 생성합니다. 다차원 내장 배열의 복사나 이동은 지원되지 않습니다.

1) 모든 i 에 대해 0, ..., N - 1 범위에서, 결과의 해당 요소를 a [ i ] 로 복사 초기화합니다. 이 오버로드는 std:: is_constructible_v < T, T & > false 일 때 형식이 올바르지 않습니다.
2) 모든 i 에 대해 0, ..., N - 1 범위에서, 결과의 해당 요소를 std :: move ( a [ i ] ) 로 이동 초기화합니다. 이 오버로드는 std:: is_move_constructible_v < T > false 일 때 형식이 잘못됩니다.

두 오버로드는 std:: is_array_v < T > true 일 때 형식이 올바르지 않습니다.

목차

매개변수

a - std::array 로 변환될 내장 배열
타입 요구사항
-
T 는 오버로드 (1) 사용을 위해 CopyConstructible 요구사항을 충족해야 함
-
T 는 오버로드 (2) 사용을 위해 MoveConstructible 요구사항을 충족해야 함

반환값

1) std:: array < std:: remove_cv_t < T > , N > { a [ 0 ] , ..., a [ N - 1 ] }
2) std:: array < std:: remove_cv_t < T > , N > { std :: move ( a [ 0 ] ) , ..., std :: move ( a [ N - 1 ] ) }

참고 사항

클래스 템플릿 인수 추론 을 사용할 수 없는 경우가 있지만 to_array 는 사용 가능한 상황이 있습니다:

  • to_array std::array 의 요소 타입을 수동으로 지정하고 길이를 추론할 때 사용할 수 있으며, 암시적 변환이 필요한 경우에 선호됩니다.
  • to_array 는 문자열 리터럴을 복사할 수 있는 반면, 클래스 템플릿 인수 추론은 첫 번째 문자를 가리키는 단일 포인터의 std::array 를 생성합니다.
std::to_array<long>({3, 4}); // OK: 암시적 변환
// std::array<long>{3, 4};   // 오류: 템플릿 인수가 너무 적음
std::to_array("foo");        // std::array<char, 4>{'f', 'o', 'o', '\0'} 생성
std::array{"foo"};           // std::array<const char*, 1>{"foo"} 생성
기능 테스트 매크로 표준 기능
__cpp_lib_to_array 201907L (C++20) std::to_array

가능한 구현

to_array (1)
namespace detail
{
    template<class T, std::size_t N, std::size_t... I>
    constexpr std::array<std::remove_cv_t<T>, N>
        to_array_impl(T (&a)[N], std::index_sequence<I...>)
    {
        return {{a[I]...}};
    }
}
template<class T, std::size_t N>
constexpr std::array<std::remove_cv_t<T>, N> to_array(T (&a)[N])
{
    return detail::to_array_impl(a, std::make_index_sequence<N>{});
}
to_array (2)
namespace detail
{
    template<class T, std::size_t N, std::size_t... I>
    constexpr std::array<std::remove_cv_t<T>, N>
        to_array_impl(T (&&a)[N], std::index_sequence<I...>)
    {
        return {{std::move(a[I])...}};
    }
}
template<class T, std::size_t N>
constexpr std::array<std::remove_cv_t<T>, N> to_array(T (&&a)[N])
{
    return detail::to_array_impl(std::move(a), std::make_index_sequence<N>{});
}

예제

#include <array>
#include <memory>
#include <string_view>
#include <type_traits>
#include <utility>
// string_view들의 constexpr 배열을 생성합니다
constexpr auto w1n = std::to_array<std::string_view>({
    "Mary", "Patricia", "Linda", "Barbara", "Elizabeth", "Jennifer"
});
static_assert(std::is_same_v<decltype(w1n), const std::array<std::string_view, 6>>);
static_assert(w1n.size() == 6 and w1n[5] == "Jennifer");
int main()
{
    // 문자열 리터럴을 복사합니다
    auto a1 = std::to_array("foo");
    static_assert(a1.size() == 4);
    // 요소 타입과 길이를 모두 추론합니다
    auto a2 = std::to_array({0, 2, 1, 3});
    static_assert(std::is_same_v<decltype(a2), std::array<int, 4>>);
    // 요소 타입을 지정하고 길이를 추론합니다
    // 암시적 변환이 발생합니다
    auto a3 = std::to_array<long>({0, 1, 3});
    static_assert(std::is_same_v<decltype(a3), std::array<long, 3>>);
    auto a4 = std::to_array<std::pair<int, float>>(
        {{3, 0.0f}, {4, 0.1f}, {4, 0.1e23f}});
    static_assert(a4.size() == 3);
    // 복사할 수 없는 std::array를 생성합니다
    auto a5 = std::to_array({std::make_unique<int>(3)});
    static_assert(a5.size() == 1);
    // 오류: 다차원 배열 복사는 지원되지 않습니다
    // char s[2][6] = {"nice", "thing"};
    // auto a6 = std::to_array(s);
}

참고 항목

(library fundamentals TS v2)
인수들로부터 크기와 선택적으로 요소 타입이 추론되는 std::array 객체를 생성합니다
(함수 템플릿)