LWG2349有什么效果? [英] What Effect Would LWG2349 Have?
问题描述
虽然libstdc ++没有,但是libc ++遵循标准,通过 ios_base :: failbit
basic_istream ::例外
对格式化输入没有影响。例如这个代码:
While libstdc++ does not, libc++ does follow the standard which states that passing ios_base::failbit
to basic_istream::exceptions
has no effect on formatted input. For example this code:
istringstream is{"ASD"};
double foo;
is.exceptions(istream::failbit);
try {
is >> foo;
cout << foo << endl;
} catch(ios_base::failure& fail) {
cout << "ouch\n";
}
将导致:
- 在libstdc ++上ouch
- libc ++上的0
- "ouch" on libstdc++
- "0" on libc++
我的阅读 LWG2349 是因为它会导致 basic_istream
不会抛出任何格式化的输入。
My reading of LWG2349 is that it would cause basic_istream
to not throw on any formatted input.
例如LWG2349提出对标准的27.7.2.3 [istream] / 1的修改,该引用参考将使libc ++的行为像libstdc ++ 的错误无效。更改为粗体,如下所示:
For example LWG2349 proposes a change to the standard's 27.7.2.3 [istream]/1 which was cited with reference to the invalidation of a bug that would have made libc++ behave like libstdc++. The change is in bold and strike through below:
如果异常除了从
(如果有的话),那么
ios :: badbit
在*这个
的错误状态。(如果basic_ios<> :: clear()
抛出的异常不会被捕获或重新抛出。))& badbit)!= 0
然后异常被重新抛出。
If an exception , other than the ones thrown from
clear()
, if any, is thrown during input thenios::badbit
is turned on in*this
’s error state.(Exceptions thrown fromIfbasic_ios<>::clear()
are not caught or rethrown.)(exceptions()&badbit) != 0
then the exception is rethrown.
我明白 basic_istream :: clear
是什么对错误的格式化输入的反应,所以我误读了LWG2349,或者它实际上是停止 basic_istream
抛出任何错误?
I understand that basic_istream::clear
is what throws in reaction to bad formatted input so am I misreading LWG2349 or is it in fact going to stop basic_istream
from throwing any errors?
推荐答案
从 throw()
抛出异常的要确保 clear()
throws,因为输入函数调用 clear(failbit)
和 (exceptions()& failbit)!= 0
,那么badbit没有被设置为结果。 clear()
将继续抛出这种情况,它不会设置badbit。
The point of the language excluding exceptions "thrown from clear()
" is to ensure that if clear()
throws, because an input function has called clear(failbit)
and (exceptions() & failbit) != 0
, then badbit is not set as a result. clear()
will continue to throw in that case, it just will not set badbit.
如对LWG2349的评论,意图是当用户代码抛出异常时设置badbit:
As described in the commentary to LWG2349, the intention is that badbit is set when an exception is thrown from user code:
PJ和Matt都同意意图(badbit + rethrow)是表示用户代码中出现的异常,而不是iostreams包。
PJ and Matt both agree that the intention (of badbit + rethrow) is "to signify an exception arising in user code, not the iostreams package".
现在,什么时候可以用户代码抛出异常,但是在iostreams机器中呢?一个例子是由locale getter:
Now, when can an exception be thrown by "user code" but within the iostreams machinery? One example would be by the locale getters:
struct my_get : std::num_get<char> {
using iter_type = std::istreambuf_iterator<char>;
iter_type do_get(iter_type, iter_type, std::ios_base&, std::ios_base::iostate&, bool&) const override {
throw std::logic_error{"my_get::do_get"};
}
};
int main() {
std::istringstream iss;
iss.imbue({std::locale{}, new my_get});
iss.exceptions(std::ios_base::failbit | std::ios_base::badbit);
try {
bool b;
iss >> b;
} catch (std::exception& ex) {
std::cout << ex.what() << '\n';
}
std::cout
<< ((iss.rdstate() & std::ios_base::eofbit) ? "eof " : "")
<< ((iss.rdstate() & std::ios_base::failbit) ? "fail " : "")
<< ((iss.rdstate() & std::ios_base::badbit) ? "bad " : "")
<< '\n';
}
目前,gcc输出:
eof fail
clang输出: p>
clang outputs:
eof fail
在LWG2349之后,正确的行为是设置badbit并抛出异常:
After LWG2349, the correct behavior is to set badbit and rethrow the exception:
my_get::do_get
eof bad
这篇关于LWG2349有什么效果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!