#include <fstream>
int main()
{
std::ifstream fin{ "" };
size_t n = fin.tellg(); // ok
}
The code compiles ok. However, according to cppreference, I find fin.tellg() is a type of std::fpos, which doesn't define the ability to convert itself to size_t implicitly.
Any explanations?
You are right about the fact that it returns an std::fpos. Now let's look at how it's defined:
template<class _Statetype>
class fpos {<...>}
fpos also does have a conversion operator for converting into the streamoff type which is "stream offset" type:
__CLR_OR_THIS_CALL operator streamoff() const
{ // return offset
return ((streamoff)(_Myoff + _FPOSOFF(_Fpos)));
}
On my machine streamoff happens to be defined as typedef _Longlong streamoff;, I would believe it's something similar on your machine. This is why it can be converted to type_t, however nothing prevents it from being larger than type_t, so be careful.
"OK" on your platform. Not necessarily OK on all platforms. Whether in practice that means much depends on the circumstances. For example, on a 32-bit system, the file may be 4GB or larger, and thus not fit in the 32-bit size_t, where std::fpos is a 64-bit value and does hold the size of the file.
If the n is used to determine the length of the file or some such, serious problems may occur if you misjudge the total size - overwriting old data, or if you load the file and then save it based on that, you'd lose some or all of the data.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With