std::array の推論ガイド
From cppreference.com
| ヘッダー <array> で定義 |
||
| template< class T, class... U > array( T, U... ) -> array<T, 1 + sizeof...(U)>; |
(C++17以降) | |
std::array のために、推論ガイドが1つ提供されています。これは、可変個引数パックからstd::arrayを構築するための、std::experimental::make_array の等価物を提供します。
(std::is_same_v<T, U> && ...) が真でない場合、プログラムはill-formedとなります。なお、sizeof...(U) がゼロの場合、(std::is_same_v<T, 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>); }