I am working with the HDF5 library and am trying to write a template function to support I/O with different data types. The goal just to reduce and simplify the copy and pasting i need to do to create all the different functions.
In the HDF5 library this is the format for the write call (documentation here):
H5Dwrite(hid_t dataset_id, hid_t mem_type_id, hid_t mem_space_id, hid_t file_space_id, hid_t xfer_plist_id, const void * buf )
Many of these parameters are generated in my function or passed as function parameters. The only problem is the mem_type_id
, which is defined as a series of #define
clauses in the header file for the library.
Is there some trick I can use to change this based on the template type? I would prefer to avoid a giant conditional statement with the std::typeid(T)
everywhere. The code is already massive, when compiled, so avoiding the bytes needed to store an unnecessary conditional seems advisable. There are over 40 types that I should implement to keep this generic for future projects. If there is anyway to limit my template from allowing types I have not defined, that might allow me to reduce the work to the minimum.
Possible solutions that I have though of include:
- Creating a specialized template function that returns the HDF5 type id, where i have a specialization for each type.
- Giant if statement