std::basic_ios::clear(3)
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 errorbadbitβ irrecoverable stream errorfailbitβ 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
- Behavior as published: an excption would be thrown if the current error state includes a bit that is also included in the
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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms