c++ - std::stringstream(设置failbit和badbit的方法)中可能存在哪些错误?

标签 c++ c++11 language-lawyer

std::stringstream 中可能存在哪些错误?

具体来说,std::stringstream 派生自 std::ios,这意味着它有一个 std::ios::rdstate >。在 std::ios::rdstate 中,当设置 failbitbadbit 时,我们会遇到问题。因此,在 std::stringstream 中设置 failbitbadbit 的可能方法是什么?

设置 failbitbadbit 的方法是依赖于编译器/实现还是由标准指定?

最佳答案

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/

相关文章:

c++ - 使用 `void*` 将右值引用绑定(bind)到左值

c++ - 在 C++ 中使用虚函数

c++ - 使用 copy_if 复制 std::string(到另一个字符串)

c++ - 带大括号的变量初始化

c++ - 在 C++11 中实现线程安全方法的正确方法

c# - 使 void 不是原始类型的设计原理是什么?

c++ - 从 <T1> 类型的对象创建 <T> 类型对象的构造函数

c++ - 从 lambda 返回对象/右值引用的首选形式

c++ - 在其他类中实现类似 std::array 的构造函数

c++ - 是否允许比较 static_assert 中静态类字段上的指针?