std::to_array
來自 cppreference.com
定義於標頭檔案 <array> |
||
template< class T, std::size_t N > constexpr std::array<std::remove_cv_t<T>, N> to_array( T (&a)[N] ); |
(1) | (C++20 起) |
template< class T, std::size_t N > constexpr std::array<std::remove_cv_t<T>, N> to_array( T (&&a)[N] ); |
(2) | (C++20 起) |
從一維內建陣列 a
建立 std::array。不支援複製或移動多維內建陣列。
1) 對於
0, ..., N - 1
中的每個 i
,使用 a[i] 複製初始化結果的對應元素。當 std::is_constructible_v<T, T&> 為 false 時,此過載格式不正確。2) 對於
0, ..., N - 1
中的每個 i
,使用 std::move(a[i]) 移動初始化結果的對應元素。當 std::is_move_constructible_v<T> 為 false 時,此過載格式不正確。當 std::is_array_v<T> 為 true 時,兩個過載都格式不正確。
目錄 |
[編輯] 引數
a | - | 要轉換為 std::array 的內建陣列 |
型別要求 | ||
-為了使用過載 (1),T 必須滿足 CopyConstructible 的要求。 | ||
-為了使用過載 (2),T 必須滿足 MoveConstructible 的要求。 |
[編輯] 返回值
1) std::array<std::remove_cv_t<T>, N>{ a[0], ..., a[N - 1] }
2) std::array<std::remove_cv_t<T>, N>{ std::move(a[0]), ..., std::move(a[N - 1]) }
[編輯] 注意
在某些情況下,當 to_array
可用時,無法使用 類模板引數推導 的 std::array
- 當手動指定
std::array
的元素型別並推導長度時,可以使用to_array
,這在需要隱式轉換時是首選。 -
to_array
可以複製字串字面量,而類模板引數推導則構造一個指向其第一個字元的單個指標的std::array
。
std::to_array<long>({3, 4}); // OK: implicit conversion // std::array<long>{3, 4}; // error: too few template arguments std::to_array("foo"); // creates std::array<char, 4>{'f', 'o', 'o', '\0'} std::array{"foo"}; // creates std::array<const char*, 1>{"foo"}
特性測試宏 | 值 | 標準 | 特性 |
---|---|---|---|
__cpp_lib_to_array |
201907L |
(C++20) | std::to_array
|
[編輯] 可能的實現
to_array (1) |
---|
namespace detail { template<class T, std::size_t N, std::size_t... I> constexpr std::array<std::remove_cv_t<T>, N> to_array_impl(T (&a)[N], std::index_sequence<I...>) { return {{a[I]...}}; } } template<class T, std::size_t N> constexpr std::array<std::remove_cv_t<T>, N> to_array(T (&a)[N]) { return detail::to_array_impl(a, std::make_index_sequence<N>{}); } |
to_array (2) |
namespace detail { template<class T, std::size_t N, std::size_t... I> constexpr std::array<std::remove_cv_t<T>, N> to_array_impl(T (&&a)[N], std::index_sequence<I...>) { return {{std::move(a[I])...}}; } } template<class T, std::size_t N> constexpr std::array<std::remove_cv_t<T>, N> to_array(T (&&a)[N]) { return detail::to_array_impl(std::move(a), std::make_index_sequence<N>{}); } |
[編輯] 示例
執行此程式碼
#include <array> #include <memory> #include <string_view> #include <type_traits> #include <utility> // creates a constexpr array of string_view's constexpr auto w1n = std::to_array<std::string_view>({ "Mary", "Patricia", "Linda", "Barbara", "Elizabeth", "Jennifer" }); static_assert(std::is_same_v<decltype(w1n), const std::array<std::string_view, 6>>); static_assert(w1n.size() == 6 and w1n[5] == "Jennifer"); int main() { // copies a string literal auto a1 = std::to_array("foo"); static_assert(a1.size() == 4); // deduces both element type and length auto a2 = std::to_array({0, 2, 1, 3}); static_assert(std::is_same_v<decltype(a2), std::array<int, 4>>); // deduces length with element type specified // implicit conversion happens auto a3 = std::to_array<long>({0, 1, 3}); static_assert(std::is_same_v<decltype(a3), std::array<long, 3>>); auto a4 = std::to_array<std::pair<int, float>>( {{3, 0.0f}, {4, 0.1f}, {4, 0.1e23f}}); static_assert(a4.size() == 3); // creates a non-copyable std::array auto a5 = std::to_array({std::make_unique<int>(3)}); static_assert(a5.size() == 1); // error: copying multidimensional arrays is not supported // char s[2][6] = {"nice", "thing"}; // auto a6 = std::to_array(s); }
[編輯] 另請參閱
(庫基礎 TS v2) |
建立 std::array 物件,其大小和可選的元素型別從引數中推導 (函式模板) |