mardi 5 octobre 2021

C++ create a read class based on template

I created several functions to read files that can have the following structure.

Files with scalar data:

0
1
2
3
4

Files with vector data:

[0,0,0]
[1,1,1]
[2,2,2]
[3,3,3]
[4,4,4]

In the script I am building a templated variable is used to return a vector of doubles or vectors (std::array <double,3>) inside another class.

template <typename vectorType, typename primitiveType>
vectorType readFile( const std::string& path)

e.g.,

vector<double> test = readFile<vector<double>, double> (PathToFile);

The code I did has some if constexpr statements to handle how to read the different templated types.

However, I would like to put the functions into a class. Based on the type of the template it should create the correct class to read the respective file. I would like to remove the if constexpr statements.

Something like:

So if I pass the templated paramenter as a vector<double>, the read class should create a class to only read the first type of data. If I pass a vector<array<double,3>> it should only read the second type of data

Is this possible to achieve? Any points on how to get started?

Best regards

Aucun commentaire:

Enregistrer un commentaire