streampos
之间有什么区别?和 pos_type
, streamoff
和 off_type
, 除了它们的定义不同。我应该将 basic_stream<>::seek
与什么一起使用?的功能?
最佳答案
std::basic_istream
和 std::basic_ostream
两者都采用两种模板类型,CharT
和 Traits
.给定一个派生自基本流之一的 A 类,Traits
数据类型可以检索为
A::traits_type
根据C++标准的§21.2,该数据类型必须提供以下成员类型:
char_type // must be identical to CharT of the basic-stream
off_type
pos_type
(以及一些与当前问题无关的其他数据类型)。鉴于 way the std::basic_istream<>::seekg()
method is defined , off_type
的预期含义和 pos_type
是:
-
pos_type
用于流中的绝对位置 -
off_type
用于相对位置
所以如果你想使用绝对版本的seekg()
,您应该声明的数据类型是 A::pos_type
(与 A::traits_type::pos_type
相同)。对于相对版本,它是 A::off_type
.
关于 std::streampos
和 std::streamoff
: 这些也由标准定义为用于 traits_type
的默认 版本的数据类型.换句话说,如果您没有明确指定 Traits
模板参数,A::pos_type
事实上会是std::streampos
, 和 A::off_type
事实上会是std::streamoff
.
如果您创建自己的 Traits
版本 并希望将它与标准库模板一起使用,例如 std::basic_istream<>
等等,您必须包含 pos_type
的类型定义和 off_type
(以及许多其他数据类型),并确保它们符合标准的 §27.2.2 和 §27.3。
关于c++ - streampos 和 pos_type、streamoff 和 off_type 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10133680/