std::get(std::tuple)(3)

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

🌸 NAME 🌸

std::get(std::tuple) β€” std::get(std::tuple) (yes bestie, that’s literally the name and it’s iconic).

okay so this is the std::get(std::tuple)(3) page from the C++ Standard Libary manual, and it’s the whole vibe for pulling one element out of a tuple. it’s giving β€œaccess by index or by type” and I’m obsessed πŸ’…βœ¨

✨ Synopsis ✨

Defined in header <tuple> πŸŽ€

here are all eight overloads, no cap, no skipping β€” every single one is below πŸ”₯

Overload (1) β€” since C++11; constexpr since C++14:

template< std::size_t I, class... Types >
typename std::tuple_element<I, tuple<Types...> >::type& get( tuple<Types...>& t ) noexcept;

Overload (2) β€” since C++11; constexpr since C++14:

template< std::size_t I, class... Types >
typename std::tuple_element<I, tuple<Types...> >::type&& get( tuple<Types...>&& t ) noexcept;

Overload (3) β€” since C++11; constexpr since C++14:

template< std::size_t I, class... Types >
typename std::tuple_element<I, tuple<Types...> >::type const& get( const tuple<Types...>& t ) noexcept;

Overload (4) β€” since C++11; constexpr since C++14:

template< std::size_t I, class... Types >
typename std::tuple_element<I, tuple<Types...> >::type const&& get( const tuple<Types...>&& t ) noexcept;

Overload (5) β€” since C++14:

template< class T, class... Types >
constexpr T& get( tuple<Types...>& t ) noexcept;

Overload (6) β€” since C++14:

template< class T, class... Types >
constexpr T&& get( tuple<Types...>&& t ) noexcept;

Overload (7) β€” since C++14:

template< class T, class... Types >
constexpr const T& get( const tuple<Types...>& t ) noexcept;

Overload (8) β€” since C++14:

template< class T, class... Types >
constexpr const T&& get( const tuple<Types...>&& t ) noexcept;

1-4) β€” Extracts the I-th element from the tuple. I must be an integer value in [0, sizeof...(Types)).

5-8) β€” Extracts the element of the tuple t whose type is T.

⚠️ warning bestie: 5-8) fails to compile unless the tuple has exactly one element of that type. like, exactly one. don’t try it with two matching types and expect a cute lil result πŸ’€

πŸ’– Parameters πŸ’–

  • t β€” tuple whose contents to extract 🫢

πŸŽ€ Return value πŸŽ€

A reference to the selected element of t. periodt πŸ’…

πŸ¦‹ Notes πŸ¦‹

feature-test macro moment, lowkey important:

  • __cpp_lib_tuples_by_type β€” value 201304L, std (C++14), feature: Addressing tuples by type ✨

🌈 Example 🌈

// Run this code

#include <iostream>
#include <string>
#include <tuple>

int main()
{
    auto t = std::make_tuple(1, "Foo", 3.14);

    // Index-based access
    std::cout << "( " << get<0>(t)
              << ", " << get<1>(t)
              << ", " << get<2>(t)
              << " )\n";

    // Type-based access (C++14 or later)
    std::cout << "( " << get<int>(t)
              << ", " << get<const char*>(t)
              << ", " << get<double>(t)
              << " )\n";

    // Note: std::tie and structured binding may also be used to decompose a tuple.
}

⭐ Output ⭐

( 1, Foo, 3.14 )
( 1, Foo, 3.14 )

πŸ’€ Defect reports πŸ’€

The following behavior-changing defect reports were applied retroactively to previously published C++ standards. no cap, they really went back and fixed it πŸ‘‘

  • DR LWG 2485 β€” applied to C++11 (by index) and C++14 (by type). Behavior as published: there are no overloads for const tuple&&. 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.
  • get(std::array) (C++11) β€” accesses an element of an array (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).
  • get(std::complex) (C++26) β€” obtains a reference to real or imaginary part from a std::complex (function template).

mwah πŸ’‹ this lil page came from http://cppreference.com, version/date 2024.06.10, and it’s std::get(std::tuple)(3) β€” k bye bestie, go extract some elements responsibly 🫢✨

πŸ“¦ where this page comes from