istream behavior change in C++ upon failure

前端 未结 2 719
离开以前
离开以前 2020-11-27 21:20

Take from: cppreference

Until C++11:

If extraction fails (e.g. if a letter was entered where a digit is expected), value is left unmodified an

2条回答
  •  自闭症患者
    2020-11-27 22:10

    It seems as originally specified, the operator>>s were broken in some cases (i.e. strictly speaking couldn't exist). This is the "fix".

    In a draft from early 2011, The Standard is basically the same in this regard as it was in 2003. However, in a library defect report opened by Matt Austern (in 1998!), num_get<>::get() doesn't exist for short and int. So they were changed to use the long version, and check the read number falls within the correct range.

    The defect report is here.

    (Doesn't really explain why they didn't think they could keep the originally intended behaviour, but it is why this part of The Standard was changed.)

提交回复
热议问题