Namespaces
Variants

std::experimental:: make_array

From cppreference.net
ヘッダーで定義 <experimental/array>
template < class D = void , class ... Types >
constexpr std:: array < VT /* see below */ , sizeof... ( Types ) > make_array ( Types && ... t ) ;
(ライブラリ基盤仕様 TS v2)

引数の数と等しいサイズを持ち、その要素が対応する引数から初期化される std::array を作成します。 std:: array < VT, sizeof... ( Types ) > { std:: forward < Types > ( t ) ... を返します。

D void の場合、導出される型 VT std:: common_type_t < Types... > です。それ以外の場合、それは D です。

D void であり、かつ std:: decay_t < Types > ... のいずれかが std::reference_wrapper の特殊化である場合、プログラムは不適格です。

目次

注記

make_array は、Library Fundamentals TS v3で削除されました。これは、 deduction guide std::array std::to_array に対してC++20ですでに導入されているためです。

実装例

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)...};
}

#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';
}

出力:

Returns an array of five ints? true

関連項目

C++ documentation for std::array deduction guides
組み込み配列から std::array オブジェクトを作成する
(関数テンプレート)