【发布时间】:2017-01-18 08:14:37
【问题描述】:
#include <fstream>
int main()
{
std::ifstream fin{ "" };
size_t n = fin.tellg(); // ok
}
代码编译正常。但是,根据cppreference,我发现fin.tellg()是std::fpos的一种类型,它并没有定义隐式转换为size_t的能力。
有什么解释吗?
【问题讨论】:
-
std::streampos是一个整数类型,它可能与您系统上的size_t兼容。仅仅因为没有保证,并不意味着它从不有效。 -
如果文件非常大,你应该特别小心这种转换。
-
理论上有可能
std::fpos是64位,而size_t是32位。 -
@BoPersson
basic_istream::tellg()返回pos_type;pos_type是Traits::pos_type;char_traits<char>::pos_type是streampos;streampos是fpos<mbstate_t>,这不是整数类型? -
@BoB - 好吧,我错过了一步。
fpos可隐式转换为streamoff,即整数类型。
标签: c++ type-conversion standards filestream