增加一个streampos对象

发布于 2024-10-27 21:45:19 字数 522 浏览 1 评论 0原文

我正在尝试做这样的事情:

for (std::streampos Position = 0; Position < 123; Position++)
{
    // Use Position to access something...
}

但是,std::streampos 似乎没有 operator++ 重载。

尝试使用 Position = (Position + 1) 会导致以下错误:

ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:

是否有任何解决方法,或者我是否必须依赖 long unsigned int 足够大对于文件?

I'm trying to do something like this:

for (std::streampos Position = 0; Position < 123; Position++)
{
    // Use Position to access something...
}

However, it appears that std::streampos does not have operator++ overloaded.

Trying to use Position = (Position + 1) results in the following error:

ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:

Is there any workaround for this, or do I have to rely on long unsigned int being big enough for files?

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

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

发布评论

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

评论(4

泪是无色的血 2024-11-03 21:45:19

尝试 std::streamoff,它表示偏移量在一个流中。它支持前置和后置递增/递减运算符。

底层类型是实现定义的,但必须能够一致地转换为streamsize和fpos(因此,也可以转换为streampos

编辑Maxpm的评论:您可以应用streamoff 到任何地方,无论是 ios::beg 还是任意 streampos。将其应用于 ios::beg ,它的行为就像普通的streampos 。将其应用到 streampos 上,您就得到了 streampos+streamoff

Try a std::streamoff, which represents an offset in a stream. It supports both pre- and post increment/decrement operators.

The underlying type is implementation defined, but must be able to be consistently converted to both streamsize and fpos (thus, to streampos too)

Edit to Maxpm's comment: You can apply the streamoff to anywhere, be it ios::beg or an arbitary streampos. Apply it to ios::beg and it behaves like a normal streampos. Apply it to a streampos and you got streampos+streamoff.

宣告ˉ结束 2024-11-03 21:45:19

使用 +=

for (std::streampos Position = 0; Position < 123; Position += 1)

+ 不起作用,因为 operator + 实际上是为 streampossteamoff 定义的,而不是int

这意味着存在两个同样好的隐式转换:您的 1 可以转换为 streamoff (这可能是 unsigned long 的 typedef )。或者将 streampos 隐式转换为 streamoff,然后添加 1

Use +=:

for (std::streampos Position = 0; Position < 123; Position += 1)

+ doesn’t work because operator + is actually defined for streampos and steamoff, not int.

This means that two implicit conversions exist that are equally good: either your 1 could be converted to streamoff (which is probably a typedef for unsigned long). Or the streampos is implicitly converted to streamoff which then has 1 added.

小姐丶请自重 2024-11-03 21:45:19

std::streampos 不是数字类型,尽管它支持转换
与数字类型之间的转换。如果你想对
位置,您需要使用 std::streamoff (并指定 from
调用seek时的参数)。

另外,不要忘记,你不能在其中寻求任意位置
一个文件,除非它是以二进制模式打开的,并且带有“C”
语言环境。

std::streampos is not a numeric type, although it supports conversion
to and from numeric types. If you want to do arithmetic on the
position, you need to use std::streamoff (and specify the from
argument when calling seek).

Also, don't forget that you can't seek to an arbitrary position in
a file unless it has been opened in binary mode, and imbued with the "C"
locale.

浅沫记忆 2024-11-03 21:45:19

我在研究如何从 std::streampos 对象中减去时遇到了这个链接:
https://bugs.debian.org/cgi-bin/bugreport。 cgi?bug=187599

建议的解决方案是使用定义的运算符重载 operator+()operator-() ,它们非常适用于 我。

Position.operator+(increment_val); 
Position.operator-(decrement_val); // alternative

聚苯乙烯
使用 operator+() 可能比使用 operator-() 更好,以避免与符号更改相关的任何问题

I came across this link while looking into how I can subtract from a std::streampos object:
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=187599

The suggested solution is using the defined operator overload operator+() or operator-() which work pretty well for me.

Position.operator+(increment_val); 
Position.operator-(decrement_val); // alternative

P.S.
It may be better to use operator+() than operator-() to avoid any problems related to sign change

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