I think your basic approach is wrong.
It seems you are trying to use template meta programming to achieve your goals.
This is probably not a good idea.
A simpler approach is to just use C ++ streams.
These stream objects already know how to read all the basic types. And anyone who wants to do something in C ++ will add the appropriate input and output operators for the stream of their class; therefore, it is quite universal that you can read any type as a key and value (with the restriction that it must fit on one line).
So, now you just need to use the standard template logic to define an operator that will read two objects of different types on the same line.
Try the following:
#include <string> #include <memory> #include <fstream> #include <sstream> #include <vector> #include <iterator> #include <algorithm> // These can be any types. typedef std::string Key; typedef int Value; // The data type to hold the data. template<typename K,typename V> class Data: public std::pair<K, V> { };
Here is the code that will read one record from one line of the file:
Note that the Data data type and this input statement are both templates and can thus prepare key / value pairs for any objects (as long as these objects know how the streams themselves are).
template<typename K,typename V> std::istream& operator>>(std::istream& stream, Data<K,V>& data) {
Using it just now means using a stream:
int main() { // The input file std::ifstream file("Plop"); // We will convert the file and store it into this vector. std::vector<Data<Key,Value> > data; // Now just copy the data from the stream into the vector. std::copy(std::istream_iterator<Data<Key,Value> >(file), std::istream_iterator<Data<Key, Value> >(), std::back_inserter(data) ); }
Note. In the above example, the key must be in one word (since it is read using a string). If you want a space as a string, you need to do extra work. But this is a question of another question.