std::basic_ios::clear(3)

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

std::basic_ios::clear(3)

okay so this is the C++ Standard Libary page for std::basic_ios::clear(3) πŸ’… β€” yes, that's the manual name exactly as it appears, no cap. It's giving "tiny member function, huge main character energy." lowkey short, highkey important.


πŸŽ€ NAME πŸŽ€

std::basic_ios::clear - std::basic_ios::clear

like, that's literally the whole NAME section. iconic. πŸ’–

✨ Synopsis ✨

void clear( std::ios_base::iostate state = std::ios_base::goodbit );

Okay so what it does: it sets the stream error state flags by assigning them the value of state. By default, it assigns std::ios_base::goodbit, which has the effect of clearing all error state flags. 🧚

And important vibe check: if rdbuf() is a null pointer (i.e. there is no associated stream buffer), then state | std::ios_base::badbit is assigned. Not me warning you β€” that's just the rule. 🫢

🌸 Parameters 🌸

  • state β€” the new error state flags setting. It can be a combination of the following constants:
    • goodbit β€” no error
    • badbit β€” irrecoverable stream error
    • failbit β€” input/output operation failed (formatting or extraction error)
    • eofbit β€” associated input sequence has reached end-of-file
The original mini-table is literally Constant + Explanation, and the rows above are every single one. πŸ’…

πŸŽ€ Return value πŸŽ€

(none) β€” yes bestie, it returns nothing. periodt.

πŸ”₯ Exceptions πŸ”₯

If the new error state includes a bit that is also included in the exceptions() mask, it throws an exception of type failure. So like, don't poke that bear unless you mean it. 😭

πŸ¦‹ Example πŸ¦‹

clear() without arguments can be used to unset the failbit after unexpected input; for std::cin.putback(c) see ungetc. iykyk.

The page says // Run this code, so here's the code:

#include <iostream>
#include <string>

int main()
{
    for (char c : {'\n', '4', '1', '.', '3', '\n', 'Z', 'Y', 'X'})
        std::cin.putback(c); // emulate user's input (not portable: see ungetc Notes)

    double n;
    while (std::cout << "Please, enter a number: " && !(std::cin >> n))
    {
        std::cin.clear();
        std::string line;
        std::getline(std::cin, line);
        std::cout << line << "\nI am sorry, but '" << line << "' is not a number\n";
    }
    std::cout << n << "\nThank you for entering the number " << n << '\n';
}

Output:

Please, enter a number: XYZ
I am sorry, but 'XYZ' is not a number
Please, enter a number: 3.14
Thank you for entering the number 3.14

πŸ› Defect reports πŸ›

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

  • DR: LWG 412 β€” Applied to: C++98
    • Behavior as published: an excption would be thrown if the current error state includes a bit that is also included in the exceptions() mask
    • Correct behavior: checks the new error state instead

ngl the original says "excption" right there, so I'm keeping it exactly β€” we don't edit the lore. πŸ’€

πŸ‘€ See also πŸ‘€

  • setstate β€” sets state flags (public member function)
  • rdstate β€” returns state flags (public member function)

mwah πŸ’‹ that's the whole page, bestie. source/version: http://cppreference.com; date: 2024.06.10; and it's the std::basic_ios::clear(3) page. periodt. 🫢

πŸ“¦ where this page comes from