I'm quite new to c++ but wanted to try to do some fancy template stuff. I am not sure if it is possible, but I am fairly confident there is a way to achieve that.
So here's the Issue: I am calling various Hardware function, which requires a number of parameters and only differ by one parameter type:
int Read(HANDLE handle, int location, char* Name, int startindex1, int endindex1,
int startindex2, int endindex2, int* rval, Astruct* callback) ;
int Read(HANDLE handle, int location, char* Name, int startindex1, int endindex1,
int startindex2, int endindex2, double* rval, Astruct* callback) ;
The Hardware Interface returns a scalar, array or matrix, depending on the values of the indexes. What i actually want to achieve, is a function, which returns an T
or vector<T>
or vector<vector<T>>
dependening on the number of parameters I pass.:
T MyReadFunction<T>(HANDLE handle, int location, char* Name,int index)
vector<T> MyReadFunction<T>(HANDLE handle, int location, char* Name,int startindex1,
int endindex1 )
vector<vector<T>> MyReadFunction<T>(HANDLE handle, int location, char* Name,int startindex1,
int endindex1
int startindex2,
int endindex2)
where T
is a basic type like int
, real
, float
, double
,etc.
Using 3 different templates with specializations is no issue, but I would love to combine it somehow.
My assumption is, that this can be achieve using template specializations, but I cant get my head around it. I think i should something like this:
template<typename T ,int... Indexes>
T MyReadFunction (HANDLE handle, int location, char* Name, Indexes... myIndex){}
template<>
int MyReadFunction (HANDLE handle, int location, char* Name, int myindex)
{
int rval = 0;
Read (handle,location,name,myindex,myindey,0,0,&rval, NULL) ;
return rval;
}
This comes as a two headed beast. I porperbly need to implement my 3 cases explicitly to avoid misusage, but also want to know, how i could achieve template specialization with variying size of the parameter pack, depending on the size of the pack.
I'am using msvc++ latest with VS 2019