xml*_*lmx 8 c++ standards type-conversion filestream
#include <fstream>
int main()
{
std::ifstream fin{ "" };
size_t n = fin.tellg(); // ok
}
Run Code Online (Sandbox Code Playgroud)
代码编译正常。但是,根据cppreference,我发现fin.tellg()是一种std::fpos,它没有定义将自身转换为size_t隐式的能力。
有什么解释吗?
你说得对,它返回一个std::fpos. 现在让我们看看它是如何定义的:
template<class _Statetype>
class fpos {<...>}
Run Code Online (Sandbox Code Playgroud)
fpos还有一个转换运算符,用于转换为streamoff“流偏移”类型:
__CLR_OR_THIS_CALL operator streamoff() const
{ // return offset
return ((streamoff)(_Myoff + _FPOSOFF(_Fpos)));
}
Run Code Online (Sandbox Code Playgroud)
在我的机器上streamoff恰好被定义为typedef _Longlong streamoff;,我相信它在你的机器上是类似的。这就是为什么它可以转换为type_t,但是没有什么可以阻止它大于type_t,所以要小心。
| 归档时间: |
|
| 查看次数: |
5044 次 |
| 最近记录: |