简体   繁体   English

从 istream 读取 bitset<0> 总是失败?

[英]Reading bitset<0> from istream always a failure?

I was recently working with std::bitset<0> and noticed something interesting with operator>> :我最近在使用std::bitset<0>并注意到operator>>一些有趣的事情:

The "specification" states that: 《规范》指出:

If no characters are extracted, is.setstate(ios_base::failbit) is called.如果没有提取字符,则调用 is.setstate(ios_base::failbit)。

Does this still apply when "all possible N characters" (with N == 0) have been read?当“所有可能的 N 个字符”(N == 0)已被读取时,这是否仍然适用? If so, does this imply that trying to read from an istream into a bitset<0> must always set failbit ?如果是这样,这是否意味着尝试从istream读取到bitset<0>必须始终设置failbit

The following unit test exhibits no consistent behavior across the compilers I had at hand:以下单元测试在我手头的编译器中没有表现出一致的行为:

BOOST_AUTO_TEST_CASE(io0) {
    try {
        std::stringstream ss{"1"};
        ss.exceptions(std::ios::badbit | std::ios::failbit);
        std::bitset<0> b;
        ss >> b;
        BOOST_TEST(false);
    } catch(...) {
        BOOST_TEST(true);
    }
}

MSVC 2019 sets the failbit, whilst GCC 7.3.0 does not! MSVC 2019 设置了故障位,而 GCC 7.3.0 没有!

The specification (no scare quotes) said the same thing ( when this question was asked ), so yes.规范(没有恐吓引号)说了同样的事情(当这个问题被问到时),所以是的。 (Of course, skipping whitespace first may provoke some other kind of failure instead.) I thought this a clear error in the standard, so I filed an issue which has since been resolved for C++20. (当然,首先跳过空格可能会引发其他类型的失败。)我认为这是标准中的一个明显错误,因此我提交了一个问题此问题已在 C++20 中得到解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM