I have a data structure like this
enum DataType_t{INT,FLOAT};
struct Data
{
DataType_T type;
void* min;
void* max;
};
The variables min and max depend on the value of type. I want to know if there is a way to create a std::map like
std::map<DataType_t, SomeFcnPtr> myMap;
myMap[INT] = ?? // Here should be a pointer to a function like int(*FcnPtr)(Data d, bool min);
myMap[FLOAT] = ?? // Here should be a pointer to a function like float(*FcnPtr)(Data d, bool min);
Is there a way to create such a map that have function pointers with different return data types?
In the end I want to use it to normalize values
float normalizedValue = (readValue - mayMap[INT](intData, true)) / (mayMap[INT](intData, false) - mayMap[INT](intData, true))
I read this post that looks really similar but didn't understand the proposed ideas, maybe you could give an example.
EDIT
I will add a little bit more explanation on what I am attempting. struct Data has a field DataType_t type. Depending on the value of type I need to properly cast the min and max fields to have their proper representations as int or float. One possible way could be
int getMinOrMaxForINT(Data aData, bool min)
{
if(min) return *((int*)aData.min));
return *((int*)aData.max));
}
and similarly
float getMinOrMaxForFLOAT(Data aData, bool min)
{
if(min) return *((float*)aData.min));
return *((float*)aData.max));
}
finally in some function processing Data variables I could do
void someFunction(int value, Data aData)
{
float normalizedValue = 0;
if(aData.type == DataType_t::INT)
{
normalizedValue = (value - getMinOrMaxForINT(aData, true)) / (getMinOrMaxForINT(aData, false) - getMinOrMaxForINT(aData, true));
}
else if(aData.type == DataType_t::FLOAT)
{
normalizedValue = (value - getMinOrMaxForFLOAT(aData, true)) / (getMinOrMaxForFLOAT(aData, false) - getMinOrMaxForFLOAT(aData, true));
}
}
as you notice the code is exactly the same for the getMinOrMaxForXXXX except for the return and cast types. I thought of using a template like
template <typename T>
T getMinOrMax(Data aData, bool min)
{
if(min) return *((T*)aData.min));
return *((T*)aData.max));
}
but the problem is how to have the map to get a pointer to a certain specialization, for example
myMap[DataType_t::INT] = //PointerTo getMinOrMax<int>
myMap[DataType_t::FLOAT] = //PointerTo getMinOrMax<float>
this could help me simplifying the function code in the process function
void someFunction(int value, Data aData)
{
float normalizedValue = (value - myMap[aData.type](aData, true)) / (myMap[aData.type](aData, false) - myMap[aData.type](aData, true));
}