class MyClass
{
void* m_value;
int m_type;
public:
template <typename T>
T GetVal(T p_default)
{
if (!m_value)
return p_default;
switch(m_type)
{
case 1: case 21:
return (T)(*((char*)m_value));
case 2: case 22:
return (T)(*((bool*)m_value));
case 3: case 23:
return (T)(*((short*)m_value));
case 4: case 24:
return (T)(*((float*)m_value));
default:
return p_default;
}
}
};
I'm dereferencing a pointer to a stored type and casting it to the templated type. It compiles file in VS without warnings.
In GCC I get compiler warnings about loss of precision/truncation whenever I add a bool as the templated type. Do you know how I can make this GCC-safe?
I've noticed VS handles this nicely for casting:T(value)
but GCC needs this:(T)value
That's just a little less useful because I can't call the specific constructor if T is a non-primitive. As a compromise I do this: (T)(value), but I'm probably getting the worst of both worlds.
Isn't the problem caused by returning a value rather than a ref? It you already have the thing stored, it should be a matter of just returning a reference to it?
Or maybe I just understand the context in which this is used.