std::array
的推导指引
来自 cppreference.cn
定义于头文件 <array> |
||
template< class T, class... U > array( T, U... ) -> array<T, 1 + sizeof...(U)>; |
(since C++17) | |
为 std::array 提供了一个推导指引,以提供与 std::experimental::make_array 等效的功能,用于从可变参数包构造 std::array
。
如果 (std::is_same_v<T, U> && ...) 不为真,则程序是非良构的。 请注意,当 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>); }