The real question is: what type of limit? The usual way to test, if there is still room, as a rule: limit - out_stream.tellp ()> = string.size () But you must make sure that the restriction has a type from which out_stream.tellp () can be subtracted.
In theory, flows cannot be convertible and comparable to an integral of a type, or that, converted to an integral type, it gives significant information. And for this, subtraction of support or comparison was required for this. In practice, I don’t think that you have to worry too much about converting to an existing integral type and monotony (although it’s possible on some exotic mainframe ...). But you cannot be sure that arithmetic will work with it, so I would prefer to convert it explicitly to a stream (which, as guaranteed, is a signed integral type). (Regardless of how you approach the problem, you will have to decide that string.size () returns the size_t that is required to be unsigned, while streamsize is required to sign.)
As for your second question: size_t is a typedef for an unsigned integral type large enough to indicate the size of any possible object, streamsize is a typedef for a signed integral type large enough to indicate the size of the "object" in the stream, streamoff is a typedef for an integral type capable of indicating the position of the byte in the file and streampos is a typedef for fpos, where there is something of a type that can be used to maintain state in the case of a multibyte stream. The standard has very few requirements regarding the relationship between them (and some of the few that it does are mathematically impossible to realize), so you are pretty much on your own.
James kanze
source share