std::array
的推导指引
從 cppreference.com
在標頭 <array> 定義
|
||
template< class T, class... U > array( T, U... ) -> array<T, 1 + sizeof...(U)>; |
(C++17 起) | |
為 std::array 提供了一個推導指引,以提供 用於從變長形參包構造 std::array
的 std::experimental::make_array 等價物。
若 (std::is_same_v<T, U> && ...) 非 true 則程序非良構。注意它在 sizeof...(U) 為零時為 true。
[編輯] 示例
運行此代碼
#include <algorithm> #include <array> #include <cassert> #include <type_traits> int main() { const int x = 10; std::array a{1, 2, 3, 5, x}; // OK 创建 std::array<int, 5> assert(a.back() == x); // std::array b{1, 2u}; // 错误,所有实参必须拥有相同类型 std::array c{std::to_array<short>({3, 2, 1})}; // C++20 设施 assert(std::ranges::equal(c, std::array{3, 2, 1})); static_assert(std::is_same_v<short, decltype(c)::value_type>); }