std::stringstream
中可能存在哪些错误?
具体来说,std::stringstream
派生自 std::ios
,这意味着它有一个 std::ios::rdstate
>。在 std::ios::rdstate
中,当设置 failbit
或 badbit
时,我们会遇到问题。因此,在 std::stringstream
中设置 failbit
和 badbit
的可能方法是什么?
设置 failbit
和 badbit
的方法是依赖于编译器/实现还是由标准指定?
最佳答案
C++11 中的表 124 指定了各个位的含义:
badbit indicates a loss of integrity in an input or output sequence (such as an irrecoverable read error from a file);
eofbit indicates that an input operation reached the end of an input sequence;
failbit indicates that an input operation failed to read the expected characters, or that an output operation failed to generate the desired characters.
至于什么操作设置这些位,这些位分散在标准的各个地方,您只需搜索掩码的出现即可找出设置和清除它的操作。
例如,设置 badbit
的一种方法是,如果您在对某个对象的 operator>>
调用期间使用 get 异常, istream。 27.7.2.2 格式化输入函数
对此进行了详细介绍。标准中还有许多其他地方给出了类似的描述。
关于c++ - std::stringstream(设置failbit和badbit的方法)中可能存在哪些错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26228985/