std::begin(std::valarray)(3)

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

🌸 NAME 🌸

std::begin(std::valarray) - std::begin(std::valarray).

Okay so bestie, this is the std::begin(std::valarray) page from the C++ Standard Libary manual, section 3. It’s giving β€œtiny iterator helper with main character energy” πŸ’….


πŸŒ€ Synopsis πŸŒ€

template< class T >                            (1) (since C++11)
/* see below */ begin( valarray<T>& v );
template< class T >                            (2) (since C++11)
/* see below */ begin( const valarray<T>& v );

The overload of std::begin for valarray returns an iterator of unspecified type referring to the first element in the numeric array. Lowkey mysterious, highkey useful ✨.

  1. The return type must:
    • meet the requirements of mutable LegacyRandomAccessIterator,
    • model contiguous_iterator (since C++20),
    • have a member type value_type, which is T, and
    • have a member type reference, which is T&.
  2. The return type must:
    • meet the requirements of constant LegacyRandomAccessIterator,
    • model contiguous_iterator (since C++20),
    • have a member type value_type, which is T, and
    • have a member type reference, which is const T&.

The iterator returned from this function is invalidated when the member function resize() is called on v or when the lifetime of v ends, whichever comes first. No cap, that timing detail matters 😭.


🌸 Parameters 🌸

  • v β€” a numeric array.

πŸŽ€ Return value πŸŽ€

Iterator to the first value in the numeric array. Slay.


πŸ¦‹ Exceptions πŸ¦‹

May throw implementation-defined exceptions.


πŸ’œ Notes πŸ’œ

Unlike other functions that take std::valarray arguments, begin() cannot accept the replacement types (such as the types produced by expression templates) that may be returned from expressions involving valarrays: std::begin(v1 + v2) is not portable, and std::begin(std::valarray<T>(v1 + v2)) has to be used instead. That’s the gotcha, bestie β€” don’t get got πŸ™ˆ.

The intent of this function is to allow range for loops to work with valarrays, not to provide container semantics. Periodt.


🎬 Example 🎬

// Run this code

#include <algorithm>
#include <iostream>
#include <valarray>

void show(const std::valarray<int>& v)
{
    std::for_each(std::begin(v), std::end(v), [](int c)
    {
        std::cout << c << ' ';
    });
    std::cout << '\n';
};

int main()
{
    const std::valarray<int> x{47, 70, 37, 52, 90, 23, 17, 33, 22, 16,
       21, 4};
    const std::valarray<int> y{25, 31, 71, 56, 21, 21, 15, 34, 21, 27,
       12, 6};

    show(x);
    show(y);

    const std::valarray<int> z{x + y};

    for (char c : z)
        std::cout << c;
}

✨ Output ✨

47 70 37 52 90 23 17 33 22 16 21 4
25 31 71 56 21 21 15 34 21 27 12 6
Hello, C++!

🩹 Defect reports 🩹

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

  • DR: LWG 2058
    • Applied to: C++11
    • Behavior as published:
      1. begin() was required to support replacement types
      2. it was unspecified when the returned iterators will be invalidated
    • Correct behavior:
      1. not required
      2. specified

πŸ‘€ See also πŸ‘€

  • std::end(std::valarray) β€” specializes std::end (C++11) (function template).

That’s the whole page, bestie 🫢 β€” source/version: http://cppreference.com, date: 2024.06.10 β€” mwah πŸ’‹

πŸ“¦ where this page comes from