std::basic_ios<CharT,Traits>:: clear
|
void
clear
(
std::
ios_base
::
iostate
state
=
std::
ios_base
::
goodbit
)
;
|
||
通过将 state 的值赋给流错误状态标志来设置它们。默认情况下,会分配 std::ios_base::goodbit ,其效果是清除所有错误状态标志。
如果 rdbuf() 是空指针(即没有关联的流缓冲区),则会赋值 state | std:: ios_base :: badbit 。
目录 |
参数
| state | - |
新的错误状态标志设置。可以是以下常量的组合:
|
返回值
(无)
异常
如果新的错误状态包含的某个位同时也包含在 exceptions() 掩码中,则抛出类型为 failure 的异常。
示例
无参数的
clear()
可用于在意外输入后取消设置
failbit
;关于
std::
cin
.
putback
(
c
)
请参阅
ungetc
。
#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'; }
输出:
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
缺陷报告
以下行为变更缺陷报告被追溯应用于先前发布的C++标准。
| 缺陷报告 | 应用于 | 发布时行为 | 正确行为 |
|---|---|---|---|
| LWG 412 | C++98 |
若当前错误状态包含的位也包含在
exceptions() 掩码中则会抛出异常 |
改为检查新的
错误状态 |
参见
|
设置状态标志
(公开成员函数) |
|
|
返回状态标志
(公开成员函数) |