std::end(std::valarray)(3)

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

okay so bestie, this is std::end(std::valarray)(3) from the C++ Standard Libary manual β€” yes, that spelling is literally in the header and we are not fixing it, we respect the iconique typo πŸ’…. It's giving β€œtiny overload with big range-for energy,” and we’re gonna go through every single detail together. 🫢


πŸ’– NAME πŸ’–

std::end(std::valarray) - literally std::end(std::valarray), no cap. That’s the whole name, bestie. ✨

✨ Synopsis ✨

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

The overload of std::end for valarray returns an iterator of unspecified type referring to the one past the last element in the numeric array. So it’s the β€œend” iterator vibe, but the exact type is a mystery β€” unspecified, not our business, moving on. πŸ’«

  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&.
lowkey important, bestie: 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. So don’t hold onto it like it’s a forever text, because it is not 😭.

πŸŽ€ Parameters πŸŽ€

  • v β€” a numeric array. That’s it. That’s the parameter. Slay. 🌸

🌸 Return value 🌸

Iterator to one past the last value in the numeric array. Classic end-of-range behavior, no notes. πŸ’–

πŸ¦‹ Exceptions πŸ¦‹

May throw implementation-defined exceptions. So like… ask your implementation, bestie. πŸ¦‹

πŸ’… Notes πŸ’…

Unlike other functions that take std::valarray arguments, end() cannot accept the replacement types (such as the types produced by expression templates) that may be returned from expressions involving valarrays: std::end(v1 + v2) is not portable, and std::end(std::valarray<T>(v1 + v2)) has to be used instead. So don’t try to be cute with std::end(v1 + v2) in portable code, because it’s giving not-portable vibes and we don’t do that. πŸ™ˆ

The intent of this function is to allow range for loops to work with valarrays, not to provide container semantics. So it’s a range-for helper, not a whole container glow-up. Periodt. πŸ’…

🌈 Example 🌈

Run this code, bestie:

// Run this code

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

int main()
{
    const std::valarray<char> va
    {
        'H', 'e', 'l', 'l', 'o',
        ',', ' ',
        'C', '+', '+', '!', '\n'
    };

    std::for_each(std::begin(va), std::end(va),
                  [](char c){ std::cout << c; });
}

⭐ Output ⭐

Hello, C++!

πŸ”₯ Defect reports πŸ”₯

The following behavior-changing defect reports were applied retroactively to previously published C++ standards. This is the β€œwe fixed it after the fact” section, and it ate. πŸ’…

  • DR LWG 2058 β€” applied to C++11
    • 1. behavior as published: end() was required to support replacement types β†’ correct behavior: 1. not required
    • 2. behavior as published: it was unspecified when the returned iterators will be invalidated β†’ correct behavior: 2. specified

πŸ‘‘ See also πŸ‘‘

  • std::begin(std::valarray) β€” overloads std::begin (C++11) (function template)

mwah πŸ’‹ β€” source: http://cppreference.com, version/date: 2024.06.10, page: std::end(std::valarray)(3). That’s the footer, bestie, and that’s the whole vibe. periodt. 🫢

πŸ“¦ where this page comes from