名前空間
変種
操作

std::experimental::make_array

From cppreference.com
 
 
 
 
ヘッダ <experimental/array> で定義
template< class D = void, class... Types >
constexpr std::array<VT /* 後述 */, sizeof...(Types)> make_array( Types&&... t );
(Library Fundamentals TS v2)

引数の数と同じサイズの、対応する引数から初期化されるstd::arrayを作成します。std::array<VT, sizeof...(Types)>{std::forward<Types>(t)...}を返します。

Dvoidの場合、推論される型VTstd::common_type_t<Types...>です。それ以外の場合はDです。

Dvoidであり、std::decay_t<Types>...のいずれかがstd::reference_wrapperの特殊化である場合、プログラムは不正形式となります。

目次

[編集] 注記

make_arrayは、std::arrayの推論ガイドおよびstd::to_arrayが既にC++20に含まれているため、Library Fundamentals TS v3で削除されました。

[編集] 可能な実装

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++ドキュメント std::arrayの推論ガイド
組み込み配列からstd::arrayオブジェクトを作成します。
(関数テンプレート) [編集]
English 日本語 中文(简体) 中文(繁體)