Namespaces
Variants

deduction guides for std::array

From cppreference.net
Definiert im Header <array>
template < class T, class ... U >
array ( T, U... ) - > array < T, 1 + sizeof... ( U ) > ;
(seit C++17)

Es wird eine Deduktionsanleitung für std::array bereitgestellt, um ein Äquivalent zu std::experimental::make_array für die Konstruktion von std::array aus einem variadischen Parameterpaket zu bieten.

Das Programm ist fehlerhaft, wenn ( std:: is_same_v < T, U > && ... ) nicht wahr ist. Beachten Sie, dass ( std:: is_same_v < T, U > && ... ) wahr ist, wenn sizeof... ( U ) null ist.

Beispiel

#include <algorithm>
#include <array>
#include <cassert>
#include <type_traits>
int main()
{
    const int x = 10;
    std::array a{1, 2, 3, 5, x}; // OK, erzeugt std::array<int, 5>
    assert(a.back() == x);
//  std::array b{1, 2u}; // Fehler, alle Argumente müssen denselben Typ haben
//  std::array<short> c{3, 2, 1}; // Fehler, falsche Anzahl von Template-Argumenten
    std::array c{std::to_array<short>({3, 2, 1})}; // C++20-Funktionalität
    assert(std::ranges::equal(c, std::array{3, 2, 1}));
    static_assert(std::is_same_v<short, decltype(c)::value_type>);
}