有人可以简单地向我解释一下 std::streampos 是什么吗?

Can someone explain to me briefly what is std::streampos?

你能给我一些 std::streampos 的例子吗? 我不确定它的用途,也不知道如何使用它。

我在github的一个项目中看到:

std::streampos pos = ss.tellg();

其中 ssstd::stringstream

我们为什么不使用 int pos = ss.tellg(),例如在这种情况下?

Why don't we use int pos = ss.tellg(), for example in this case?

因为 std::streampos 恰好是 return 由 std::basic_stringstream<char, std::char_traits<char>, std::allocator<char>>::tellg() 编辑的类型。

也许在一台计算机上它可以完全转换为 int,但在另一台计算机上则不行。通过使用正确的类型,您的代码与平台无关。

另请注意,std::streampos 是 return 由 class 的 tellg() 方法编辑的类型,而不是 return 编辑的类型tellg() 方法无处不在。其他流很可能 return 是一种不同的类型而不是 std::stream_pos,你应该考虑到这一点。

pos 选择正确类型的实际最简洁的方法是直接询问类型:“我应该使用什么来表示流中的位置?”:

std::stringstream::pos_type pos = ss.tellg();

或者直接使用 auto 这样你就不用担心了:

auto pos = some_stream.tellg();