Spec-Zone.ru › C++

руководства по выводу типов для std::array

Определён в заголовке <array>
template< class T, class... U >
array( T, U... ) -> array<T, 1 + sizeof...(U)>;
(начиная с C++17)

Для std::array предоставлено одно руководство по выводу типов, обеспечивающее эквивалент std::experimental::make_array при создании std::array из пакета параметров переменной длины.

Программа некорректна, если условие (std::is_same_v<T, U> && ...) не выполняется. Обратите внимание, что (std::is_same_v<T, U> && ...) истинно, если sizeof...(U) равно нулю.

Пример

#include <algorithm>
#include <array>
#include <cassert>
#include <type_traits>

int main()
{
    const int x = 10;
    std::array a{1, 2, 3, 5, x}; // OK, creates std::array<int, 5>
    assert(a.back() == x);

//  std::array b{1, 2u}; // Error, all arguments must have the same type

//  std::array<short> c{3, 2, 1}; // Error, wrong number of template args
    std::array c{std::to_array<short>({3, 2, 1})}; // C++20 facility
    assert(std::ranges::equal(c, std::array{3, 2, 1}));
    static_assert(std::is_same_v<short, decltype(c)::value_type>);
}

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/cpp/container/array/deduction_guides

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API