是否保证标准提取运算符>>在失败的情况下不会更改参数?
Is it guaranteed that standard extraction operator>> does not change argument in case of failure?
如果调用input_stream >> i;
之类的,其中i
是算术类型,抛出异常或设置badbit等,是否保证i
没有改变?
参考 std::basic_istream::operator>>
std::num_get::get, std::num_get::do_get
的 cppreference 文档:
1-4) Behaves as a FormattedInputFunction. After constructing and checking the sentry object, which may skip leading whitespace, extracts an integer value by calling std::num_get::get()
然后
Stage 3: conversion and storage:
[...]
- If the conversion function fails to convert the entire field, the value 0 is stored in v
在C++11之前,该值保持原样,[reference]:
If extraction fails (e.g. if a letter was entered where a digit is expected), value
is left unmodified and failbit
is set. (until C++11)
但在 C++11 之后,没有。如果提取失败,则设置为 0
(相同参考):
If extraction fails, zero is written to value
and failbit
is set. If extraction results in the value too large or too small to fit in value, std::numeric_limits<T>::max()
or std::numeric_limits<T>::min()
is written and failbit
flag is set. (since C++11)
如果调用input_stream >> i;
之类的,其中i
是算术类型,抛出异常或设置badbit等,是否保证i
没有改变?
参考 std::basic_istream::operator>>
std::num_get::get, std::num_get::do_get
的 cppreference 文档:
1-4) Behaves as a FormattedInputFunction. After constructing and checking the sentry object, which may skip leading whitespace, extracts an integer value by calling std::num_get::get()
然后
Stage 3: conversion and storage:
[...]
- If the conversion function fails to convert the entire field, the value 0 is stored in v
在C++11之前,该值保持原样,[reference]:
If extraction fails (e.g. if a letter was entered where a digit is expected),
value
is left unmodified andfailbit
is set. (until C++11)
但在 C++11 之后,没有。如果提取失败,则设置为 0
(相同参考):
If extraction fails, zero is written to
value
andfailbit
is set. If extraction results in the value too large or too small to fit in value,std::numeric_limits<T>::max()
orstd::numeric_limits<T>::min()
is written andfailbit
flag is set. (since C++11)