Namespaces
Variants

deduction guides for std::array

From cppreference.net
헤더 파일에 정의됨 <array>
template < class T, class ... U >
array ( T, U... ) - > array < T, 1 + sizeof... ( U ) > ;
(C++17부터)

하나의 deduction guide std::array 에 제공되어 variadic parameter pack 에서 std::array 를 생성하기 위한 std::experimental::make_array 와 동등한 기능을 제공합니다.

프로그램은 ( std:: is_same_v < T, U > && ... ) 가 참이 아닐 경우 형식이 올바르지 않습니다(ill-formed). 참고로 ( std:: is_same_v < T, U > && ... ) sizeof... ( U ) 가 0일 때 참입니다.

예제

#include <algorithm>
#include <array>
#include <cassert>
#include <type_traits>
int main()
{
    const int x = 10;
    std::array a{1, 2, 3, 5, x}; // OK, std::array<int, 5> 생성
    assert(a.back() == x);
//  std::array b{1, 2u}; // 오류: 모든 인자는 동일한 타입을 가져야 함
//  std::array<short> c{3, 2, 1}; // 오류: 템플릿 인자 개수가 잘못됨
    std::array c{std::to_array<short>({3, 2, 1})}; // C++20 기능
    assert(std::ranges::equal(c, std::array{3, 2, 1}));
    static_assert(std::is_same_v<short, decltype(c)::value_type>);
}