std::match_results::ready(3)

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

✨ NAME ✨

std::match_results::ready β€” the one that tells you if your match results are actually, you know, ready πŸ’…

okay so real quick β€” this page lives in the C++ Standard Libary manual, and yes bestie we're talking about std::match_results::ready today πŸŽ€


🌸 Synopsis 🌸

bool ready() const;  (since C++11)

It literally just tells you whether the match results are ready (valid) or not πŸ¦‹

A default-constructed match result has no result state (so it is not ready), and the only way to make it ready is by handing it to one of the regex algorithms. Being in the ready state means all the match results have been fully established β€” like, fully cooked, no notes πŸ”₯

⚠️ and here's the part where you pay attention, bestie: if you call most of the member functions on a match_results object that is not ready, the result is undefined. Undefined!! Not "returns garbage", not "throws", undefined. So don't do that πŸ’€

πŸ‘‘ Return value πŸ‘‘

  • true β€” the match results are ready ✨
  • false β€” they are not πŸ™ˆ

πŸ’– Example πŸ’–

Run this code, it's giving tutorial but make it cute:

#include <iostream>
#include <regex>
#include <string>

int main()
{
    std::string target("pattern");
    std::smatch sm;
    std::cout << "default constructed smatch is "
              << (sm.ready() ? "ready\n" : "not ready\n");

    std::regex re1("tte");
    std::regex_search(target, sm, re1);

    std::cout << "after search, smatch is "
              << (sm.ready() ? "ready\n" : "not ready\n");
}

⭐ Output ⭐

default constructed smatch is not ready
after search, smatch is ready

see?? the default-constructed one is not ready, then std::regex_search does its little main character thing and boom β€” ready πŸ’…βœ¨


🫢 that's the whole page, no cap. none of the facts got skipped, we just made them slay.

🌈 source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: std::match_results::ready(3) β€” mwah, thanks for reading bestie, go forth and check your .ready() before you touch anything else πŸ’œπŸŽ€

πŸ“¦ where this page comes from