本文介绍了streampos和pos_type,streamoff和off_type之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 //必须与basic-stream $的CharT相同b $ b off_type 
pos_type

(以及与当前问题无关的一些其他数据类型) 。考虑到, off_type pos_type 的预期含义是: p>


  • 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 / em> std :: streamoff



如果您创建自己的版本 Traits ,并希望使用标准库模板如 std :: basic_istream<> 包括 pos_type off_type (以及许多其他数据类型)的typedef,并确保它们符合§27.2。 2和§2​​7.3。


What are the differences between streampos and pos_type, streamoff and off_type, except they are defined differently. What should I use with the basic_stream<>::seek's functions?

解决方案

std::basic_istream and std::basic_ostream both take two template types, CharT and Traits. Given a class A that is derived from one of the basic-streams, the Traits data type can be retrieved as

A::traits_type

According to §21.2 of the C++ standard, this data type must provide the following member types:

char_type // must be identical to CharT of the basic-stream
off_type
pos_type

(and some further data types irrelevant to the present question). Given the way the std::basic_istream<>::seekg() method is defined, the intended meaning of off_type and pos_type is:

  • pos_type is used for absolute positions in the stream
  • off_type is used for relative positions

So if you want to use the absolute version of seekg(), the data type you should declare is A::pos_type (which is the same as A::traits_type::pos_type). For the relative version it is A::off_type.

Regarding std::streampos and std::streamoff: These are defined, too, by the standard as the data types that are used for the default version of the traits_type. In other words, if you do not explicitly specify the Traits template parameter, the A::pos_type will in fact be std::streampos, and A::off_type will in fact be std::streamoff.

If you create your own version of Traits and want to use it with standard library templates like std::basic_istream<> etc., you must include typedefs for pos_type and off_type (and a lot of other data types), and ensure they comply with §27.2.2 and §27.3 of the standard.

这篇关于streampos和pos_type,streamoff和off_type之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 10:55