std::get(std::array)(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

✨ NAME ✨

okay so bestie, this is the std::get(std::array)(3) page from the C++ Standard Libary β€” yes, that’s how the header spells it, and we’re not fixing it. πŸ’…

std::get(std::array) - std::get(std::array)

This is the tiny icon accessor that pulls the I^th element out of a std::array using the tuple-like interface. It’s giving β€œsmall function, huge main character energy.” πŸŽ€

🌈 Synopsis 🌈

Defined in header <array>

template< std::size_t I, class T, std::size_t N >    (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 >    (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 >    (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 >    (4) (since C++11)
const T&& get( const std::array<T,N>&& a ) noexcept;     (constexpr since C++14)

It extracts the I^th element from the array using the tuple-like interface. No cap. The I must be an integer value in range [0, N). This is enforced at compile time as opposed to at() or operator[] β€” so yeah, the compiler is the bouncer here. πŸšͺ✨

🌸 Parameters 🌸

  • a β€” array whose contents to extract

🫢 Return value 🫢

A reference to the I^th element of a.

⚑ Complexity ⚑

Constant. Slay.

πŸ“ Example πŸ“

The page says // Run this code, so bestie, run it:

#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)

πŸ’€ Defect reports πŸ’€

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

  • DR: LWG 2485 β€” Applied to: C++11 β€” Behavior as published: there are no overloads for const array&& β€” Correct behavior: the overloads are added

πŸ‘€ See also πŸ‘€

  • Structured binding (C++17) β€” binds the specified names to sub-objects or tuple elements of the initializer
  • operator[] β€” access specified element (public member function)
  • at β€” access specified element with bounds checking (public member function)
  • get(std::tuple) (C++11) β€” tuple accesses specified element (function template)
  • get(std::pair) (C++11) β€” accesses an element of a pair (function template)
  • get(std::variant) (C++17) β€” reads the value of the variant given the index or the type (if the type is unique), throws on error (function template)
  • get(std::ranges::subrange) (C++20) β€” obtains iterator or sentinel from a std::ranges::subrange (function template)

this page came from http://cppreference.com on 2024.06.10, and it’s the std::get(std::array)(3) page. mwah πŸ’‹

πŸ“¦ where this page comes from