While writing a generic function taking a reference to an std::streambuf, I'd like to check if the supplied buffer is tied to something supporting random access or not and optimize the treatment accordingly, i.e. check if moving around the stream with pubseekpos() is available or not.
I might have overlooked that in the documentation. Would you happen to have a crystal clear solution, other than late-discovering if it works or not based on the result of the latter method (which returns -1 in case of failure to seek, which could have other causes)?
Available docs:
- https://www.cplusplus.com/reference/streambuf/streambuf/pubseekpos/
- https://en.cppreference.com/w/cpp/io/basic_streambuf/pubseekpos
Thanks in advance. Regards.
(Edit #GIJD after first comment)
Even if I'm not really glad about it, you've got a good point here. The same as "rather than testing if a file exists before opening it, open it and process error_status/exceptions" (as a race condition is always possible between the test and the opening otherwise).
OK, the actual function call by itself might indeed be the best proof over some feature flag existence test (that might also be wrongly set, even if unlikely if the code was tested duly... well, I just read what I wrote, yeah, well, as if code is always duly tested... OK ha ha, better test the function directly yes yes).
But, still, a -1 return value might mean error, not lack of random access, but one might argue that the result is the same, I won't be able to do random access if it fails whatever the reason, feature lack or some error. Thus, I'll have to fall back to a one-pass stream read anyway.
Thanks. Regards.