为什么 istream_iterator抛出 std::bad_cast?

发布于 2024-09-16 21:02:57 字数 457 浏览 2 评论 0原文

到底是怎么回事?

#include <iostream>
#include <iterator>
#include <sstream>

int main() {
    std::basic_stringbuf<unsigned char> buf;
    std::basic_istream<unsigned char> stream(&buf);
    // the next line throws std::bad_cast on g++ 4.4
    std::istream_iterator<unsigned char, unsigned char> it(stream);
}

我在构造迭代器之前尝试过stream.write(some_array, sizeof(some_array),但无济于事。

谢谢。

What is going on?

#include <iostream>
#include <iterator>
#include <sstream>

int main() {
    std::basic_stringbuf<unsigned char> buf;
    std::basic_istream<unsigned char> stream(&buf);
    // the next line throws std::bad_cast on g++ 4.4
    std::istream_iterator<unsigned char, unsigned char> it(stream);
}

I've tried stream.write(some_array, sizeof(some_array) before constructing the iterator, to no avail.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绿光 2024-09-23 21:02:57

它从哨兵对象的构造函数中抛出,在该构造函数中检查流上的 ctype 方面(它需要它,以便可以跳过空格),它恰好是 NULL,因为它没有为无符号字符定义。

您需要处理该流上的空白吗?如果没有,则更改为

std::istreambuf_iterator<unsigned char> it(stream);

It throws from sentry object's constructor where it checks the ctype facet on the stream (it needs it so it can skip whitespace), which happens to be NULL because it's not defined for unsigned chars.

Do you need to handle whitespace on that stream? If not, change to

std::istreambuf_iterator<unsigned char> it(stream);
回心转意 2024-09-23 21:02:57

不应该是:

std::istream_iterator<unsigned char> it(stream);

shouldnt it be:

std::istream_iterator<unsigned char> it(stream);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文