I am working on a modular data logger that allows one to log data of different types. At the moment I made a File class that is a template. In order to declare an object of such a class one would do as such: File<double> f("filename.txt")
or File<float> f("filename.txt")
. I want to be able to store objects that were declared with double
or float
as template parameters in one vector. Is it possible to do something like that? I have tried a method online that uses a union as such:
union typ {
int int_dat;
double double_dat;
float float_dat;
}
and allows me to declare a vector as such: vector<File<typ> >
. However, this gives me linker errors. Is there a easier, cleaner way to attempt this? The entire project in question is here
EDIT: follow up to this. How would one circumvent the issue surrounding the fact that if I conduct such operations:
std::vector<File<typ> > files;
File<typ> f("test.txt");
files.push_back(f);
files.at(0) << 35.4;
it causes a compile time error which I comprehended as what I'm guessing is: 35.4 is not of the type typ and cannot be used in the operation <<
. How would one bypass such an error?