std::get(std::array)
From cppreference.com
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Containers library
(C++17)
(C++11)
(C++26)
(C++26)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++23)
(C++23)
(C++23)
(C++23)
(C++20)
(C++23)
Tables
std::array
Non-member functions
Helper classes
Deduction guides (C++17)
(until C++20)(until C++20)(until C++20)(until C++20)(until C++20)
Defined in header
<array>
template< std::size_t I, class T, std::size_t N >
T& get( std::array <T,N>& a ) noexcept;
(1)
(since C++11) T& get( std::array <T,N>& a ) noexcept;
(constexpr since C++14)
template< std::size_t I, class T, std::size_t N >
T&& get( std::array <T,N>&& a ) noexcept;
(2)
(since C++11) T&& get( std::array <T,N>&& a ) noexcept;
(constexpr since C++14)
template< std::size_t I, class T, std::size_t N >
const T& get( const std::array <T,N>& a ) noexcept;
(3)
(since C++11) const T& get( const std::array <T,N>& a ) noexcept;
(constexpr since C++14)
template< std::size_t I, class T, std::size_t N >
const T&& get( const std::array <T,N>&& a ) noexcept;
(4)
(since C++11) const T&& get( const std::array <T,N>&& a ) noexcept;
(constexpr since C++14)
Extracts the I
th element from the array using tuple-like interface.
I
must be an integer value in range [
0,
N)
. This is enforced at compile time as opposed to at() or operator[] .
[edit] Parameters
a
-
array whose contents to extract
[edit] Return value
A reference to the I
th element of a.
[edit] Complexity
Constant.
[edit] Example
Run this code
#include <array> #include <iostream> constexpr std::array v{1, 2, 3}; static_assert(get<0>(v) == 1 && get<1>(v) == 2 && get<2>(v) == 3); int main() { std::array <int, 3> a; // set values: get<0>(a) = 1, get<1>(a) = 2, get<2>(a) = 3; // get values: std::cout << '(' << get<0>(a) << ',' << get<1>(a) << ',' << get<2>(a) << ")\n"; }
Output:
(1,2,3)
[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 2485 | C++11 | there are no overloads for const array&& | the overloads are added |
[edit] See also
Structured binding (C++17)
binds the specified names to sub-objects or tuple elements of the initializer[edit]
(C++17)
(function template) [edit]
(C++26)
(function template) [edit]