-
Notifications
You must be signed in to change notification settings - Fork 982
Update <array> functions reference
#5684
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
09c5a32
b0d9bb4
e30b46e
183ce44
99323ab
d1f2f14
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,40 +1,45 @@ | ||
| --- | ||
| title: "<array> functions" | ||
| description: "Learn more about: <array> functions" | ||
| ms.date: 11/04/2016 | ||
| f1_keywords: ["array/std::array::get", "array/std::get", "array/std::swap"] | ||
| helpviewer_keywords: ["std::array [C++], get", "std::get [C++]", "std::swap [C++]"] | ||
| ms.date: 08/20/2025 | ||
| f1_keywords: ["array/std::array::get", "array/std::get", "array/std::swap", "array/std::to_array"] | ||
| helpviewer_keywords: ["std::array [C++], get", "std::get [C++]", "std::swap [C++]", "std::to_array [C++]"] | ||
| --- | ||
| # `<array>` functions | ||
|
|
||
| The `<array>` header includes two non-member functions, `get` and `swap`, that operate on **array** objects. | ||
| The `<array>` header includes three non-member functions, `get`, `swap`, and `to_array` that operate on **array** objects. | ||
|
|
||
| ## <a name="get"></a> `get` | ||
|
|
||
| Returns a reference to the specified element of the array. | ||
|
|
||
| ```cpp | ||
| template <int Index, class T, size_t N> | ||
| constexpr T& get(array<T, N>& arr) noexcept; | ||
| template <std::size_t Index, class Type, std::size_t Size> | ||
| constexpr T& get(std::array<Type, Size>& arr) noexcept; | ||
|
|
||
| template <int Index, class T, size_t N> | ||
| constexpr const T& get(const array<T, N>& arr) noexcept; | ||
| template <std::size_t Index, class Type, std::size_t Size> | ||
| constexpr const T& get(const std::array<Type, Size>& arr) noexcept; | ||
|
|
||
| template <int Index, class T, size_t N> | ||
| constexpr T&& get(array<T, N>&& arr) noexcept; | ||
| template <std::size_t Index, class Type, std::size_t Size> | ||
| constexpr T&& get(std::array<Type, Size>&& arr) noexcept; | ||
|
|
||
| template <std::size_t Index, class Type, std::size_t Size> | ||
| constexpr const T&& get(const std::array<Type, Size>&& arr) noexcept; | ||
|
Comment on lines
+18
to
+27
|
||
| ``` | ||
|
|
||
| ### Parameters | ||
| ### Template parameters | ||
|
|
||
| *`Index`*\ | ||
| The element offset. | ||
|
|
||
| *`T`*\ | ||
| *`Type`*\ | ||
| The type of an element. | ||
|
|
||
| *`N`*\ | ||
| *`Size`*\ | ||
| The number of elements in the array. | ||
|
|
||
| ### Parameters | ||
|
|
||
| *`arr`*\ | ||
| The array to select from. | ||
|
|
||
|
|
@@ -75,18 +80,20 @@ int main() | |
| A non-member template specialization of `std::swap` that swaps two **array** objects. | ||
|
|
||
| ```cpp | ||
| template <class Ty, std::size_t N> | ||
| void swap(array<Ty, N>& left, array<Ty, N>& right); | ||
| template <class Type, std::size_t Size> | ||
| void swap(std::array<Type, Size>& left, std::array<Type, Size>& right); | ||
| ``` | ||
|
|
||
| ### Parameters | ||
| ### Template parameters | ||
|
|
||
| *`Ty`*\ | ||
| *`Type`*\ | ||
| The type of an element. | ||
|
|
||
| *`N`*\ | ||
| *`Size`*\ | ||
| The size of the array. | ||
|
|
||
| ### Parameters | ||
|
|
||
| *`left`*\ | ||
| The first array to swap. | ||
|
|
||
|
|
@@ -143,6 +150,74 @@ int main() | |
| 0 1 2 3 | ||
| ``` | ||
|
|
||
| ## <a name="to_array"></a> `to_array` | ||
|
|
||
| Converts a built-in array to a `std::array` object. | ||
|
|
||
| ```cpp | ||
| // C++20 | ||
| template <class Type, std::size_t Size> | ||
| constexpr std::array<std::remove_cv_t<Type>, Size> to_array(Type (&arr)[Size]); | ||
|
|
||
| // C++20 | ||
| template <class Type, std::size_t Size> | ||
| constexpr std::array<std::remove_cv_t<Type>, Size> to_array(Type (&&arr)[Size]); | ||
| ``` | ||
|
|
||
| ### Template parameters | ||
|
|
||
| *`Type`*\ | ||
| The type of an element. | ||
|
|
||
| *`Size`*\ | ||
| The size of the input array. | ||
|
|
||
| ### Parameters | ||
|
|
||
| *`arr`*\ | ||
| The input array used for conversion. | ||
|
|
||
| ### Example | ||
|
|
||
| ```cpp | ||
| // std_to_array.cpp | ||
| // Requires /std:c++20 or later | ||
|
|
||
| #include <array> | ||
| #include <iostream> | ||
|
|
||
| int main() | ||
| { | ||
| int arr1[]{ 1, 2, 3 }; | ||
| std::array<int, 3> arr2 = std::to_array(arr1); | ||
|
|
||
| std::cout << "std::to_array(arr1):\n"; | ||
| for (const auto& i : arr2) | ||
| { | ||
| std::cout << i << " "; | ||
| } | ||
| std::cout << std::endl; | ||
|
|
||
| // The size is 7 as it includes the null terminator | ||
| std::array<char, 7> arr3 = std::to_array("string"); | ||
|
|
||
| std::cout << "\nstd::to_array(\"string\"):\n"; | ||
| for (const auto& i : arr3) | ||
| { | ||
| std::cout << i << " "; | ||
| } | ||
| std::cout << std::endl; | ||
| } | ||
| ``` | ||
|
|
||
| ```Output | ||
| std::to_array(arr1): | ||
| 1 2 3 | ||
|
|
||
| std::to_array("string"): | ||
| s t r i n g | ||
| ``` | ||
|
|
||
| ## See also | ||
|
|
||
| [`<array>`](../standard-library/array.md) | ||
| [`<array>`](array.md) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return types use
Tbut the template parameter is namedType. The return types should beType&,const Type&,Type&&, andconst Type&&respectively to match the template parameter name.