未指定对ostream operator<<的调用是否会失败或引发任何异常,而我从未遇到过此异常。
struct MyClass {
int data;
// I/O operators with noexcept specifier
friend std::istream& operator>>(std::istream &in, MyClass &obj) noexcept;
friend std::ostream& operator<<(std::ostream &out, const MyClass &obj) noexcept;
};
[operator >>
和operator <<
均未标记为noexcept
的原因是由于std::basic_ios::exceptions
。从std::basic_ios::exceptions
继承的所有对象中都存在此成员函数,并允许您配置流以抛出某些故障模式的异常。如果运算符为std::basic_ios
,则不能将它们与设置了异常的流一起使用。