Namespaces
Variants

std::experimental:: make_array

From cppreference.net
Definiert im Header <experimental/array>
template < class D = void , class ... Types >
constexpr std:: array < VT /* siehe unten */ , sizeof... ( Types ) > make_array ( Types && ... t ) ;
(Library Fundamentals TS v2)

Erstellt ein std::array dessen Größe der Anzahl der Argumente entspricht und dessen Elemente aus den entsprechenden Argumenten initialisiert werden. Gibt std:: array < VT, sizeof... ( Types ) > { std:: forward < Types > ( t ) ... } zurück.

Wenn D void ist, dann ist der abgeleitete Typ VT std:: common_type_t < Types... > . Andernfalls ist es D .

Wenn D void ist und einer der Typen std:: decay_t < Types > ... eine Spezialisierung von std::reference_wrapper ist, ist das Programm fehlerhaft.

Inhaltsverzeichnis

Hinweise

make_array wurde in Library Fundamentals TS v3 entfernt, da der Deduction Guide für std::array und std::to_array bereits in C++20 enthalten sind.

Mögliche Implementierung

namespace details
{
    template<class> struct is_ref_wrapper : std::false_type{};
    template<class T> struct is_ref_wrapper<std::reference_wrapper<T>> : std::true_type{};
    template<class T>
    using not_ref_wrapper = std::negation<is_ref_wrapper<std::decay_t<T>>>;
    template<class D, class...> struct return_type_helper { using type = D; };
    template<class... Types>
    struct return_type_helper<void, Types...> : std::common_type<Types...>
    {
        static_assert(std::conjunction_v<not_ref_wrapper<Types>...>,
                      "Types cannot contain reference_wrappers when D is void");
    };
    template<class D, class... Types>
    using return_type = std::array<typename return_type_helper<D, Types...>::type,
                                   sizeof...(Types)>;
}
template<class D = void, class... Types>
constexpr details::return_type<D, Types...> make_array(Types&&... t)
{
    return {std::forward<Types>(t)...};
}
**Hinweis:** Der gesamte Code innerhalb der `
` und `` Tags wurde gemäß den Anweisungen nicht übersetzt, da es sich um C++-Code handelt. Die HTML-Struktur und Attribute bleiben ebenfalls unverändert.

Beispiel

#include <experimental/array>
#include <iostream>
#include <type_traits>
int main()
{
    auto arr = std::experimental::make_array(1, 2, 3, 4, 5);
    bool is_array_of_5_ints = std::is_same<decltype(arr), std::array<int, 5>>::value;
    std::cout << "Returns an array of five ints? ";
    std::cout << std::boolalpha << is_array_of_5_ints << '\n';
}

Ausgabe:

Returns an array of five ints? true

Siehe auch

C++-Dokumentation für std::array Deduction Guides
erstellt ein std::array -Objekt aus einem eingebauten Array
(Funktionstemplate)