I have a set of classes that derive from basic_istream
and ios_base
like
std::stringstream
,boost::interprocess::basic_vectorstream
,boost::interprocess::bufferstream
At some point all those instances need to dump their content (emulated in the example below with a simple printf
):
#include <iostream>
#include <sstream>
void dump(std::basic_istream<char> &input)
{
const int chunk_size = 10;
char chars[chunk_size];
while (!input.eof()) {
input.read(chars, chunk_size);
int nread = input.gcount();
printf("CHUNK: %.*s\n", nread, chars);
}
}
int main()
{
std::stringstream ss;
ss << "Add some content. ";
ss << "And then some more." << "... and that's it";
foo(ss);
}
Since all is needed for the dump
function is eof
, read
and gcount
, a reference to the base class is passed to dump, so I'm assuming:
- All three methods are non virtual so the correct thing is called with all the needed state.
- There is no memory corruption implications (tried to verify by compiling the demo above with address sanitizer).
Are these assumptions correct and is there a case they'd break down?
I need to scale this logic for multiple other stream classes that are available in the codebase so the only red flag I can find is when a class redefines any of those methods, which would cause the reference to base class to break expectations.