I'm trying to implement a templated reader class in g++ --std=c++17 that doesn't pull in all the various system headers for whatever stream-type I'm reading.
I can use the pimpl pattern to separate the template parameters from a base implementation, but I feel like I should be able avoid that by somehow specifying that the compilation unit's definition for that function won't change depending on the template parameter.
I can also use explicit specialization but defining every specialization is too onerous.
In the header:
template<typename Parser>
class FileStream
{
Parser& parser;
int fd;
FileStream::FileStream(const char* filename, Parser& parser)
: parser(parser)
{
init(filename);
}
void init(const char* filename); // Preferably, implementation would go into .cpp file since this function does not use any template parameters
void step(char* buf, size_t len)
{
// This function needs to be in the header because it uses parser
parser.processMessage(buf, len);
}
};
In the .cpp file:
// Let's try to not include these headers in the template definition header
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
template< /* ??? */ >
void FileStream::init(const char* filename)
{
// Note: No template parameters are used in this function
fd = open(filename, O_RDONLY);
}