std::ranges..._view::pred(3)

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

std::ranges..._view::pred(3) πŸ’…

okay so bestie, this is the std::ranges..._view::pred(3) page from the C++ Standard Libary manual β€” yes, that's the manual name, typo and all, and we are not fixing it because it's iconic. It's all about std::ranges::drop_while_view::pred, which is a tiny little accessor with a very serious little warning sticker on it. πŸŽ€


NAME πŸ‘‘

std::ranges::drop_while_view::pred - std::ranges::drop_while_view::pred

Synopsis πŸ¦‹

constexpr const Pred& pred() const;  (since C++20)

The way this works: it hands you back a reference to the stored predicate. That's the whole main character move. 🫢

🚨 undefined behavior alert, fr fr: if *this does not store a predicate (e.g. an exception is thrown on the assignment to *this, which copy-constructs or move-constructs a Pred), the behavior is undefined πŸ˜­πŸ’€. That is not a vibe check you want to fail. Do not rely on it, and please do not go poking it on purpose.

Parameters 🫢

(none) β€” literally none, bestie 😌

Return value ✨

A reference to the stored predicate. That's it. That's the whole return value. Slay. πŸ’–

Example 🌸

bestie, here's the code, and yes, keep every character exactly as it is β€” this is not a drill πŸŽ€

// Run this code

#include <array>
#include <iomanip>
#include <iostream>
#include <ranges>

int main()
{
    constexpr std::array data{0, -1, -2, 3, 1, 4, 1, 5};

    auto view = std::ranges::drop_while_view
    {
        data, [](int x) { return x <= 0; }
    };

    std::cout << std::boolalpha;
    for (int x : data)
        std::cout << "predicate(" << std::setw(2) << x << ") : "
                  << view.pred()(x) << '\n';
}

Output: πŸ’€

predicate( 0) : true
predicate(-1) : true
predicate(-2) : true
predicate( 3) : false
predicate( 1) : false
predicate( 4) : false
predicate( 1) : false
predicate( 5) : false

okay bestie, that's the page! source/version and date for the footer: http://cppreference.com Β· 2024.06.10 β€” mwah, periodt πŸ’‹βœ¨πŸ«Ά

πŸ“¦ where this page comes from