My group maintains some legacy container classes, which stores both pointers and 32-bit integers. When they were written pointers were 32-bits but they're now 64-bits. We have two versions of every function, one for pointers and one for integers, which I'm trying to merge into one function using templates. How can I do this and make the compiler happy? A toy version of the problem is below.
void* mPtr = nullptr;
template<class T>
void Func(T t) {
mPtr = reinterpret_cast<void*>(t);
}
template<class T>
T Func2() {
if (typeid(T) == typeid(int))
return static_cast<int>(reinterpret_cast<long long>(mPtr));
else
return static_cast<T>(mPtr);
}
class MyClass {};
int main() {
MyClass someClass;
Func(&someClass);
MyClass* myClass = Func2<MyClass*>();
int val = Func2<int>();
}
Please remember that this is just a toy problem and I'm only interested in answers here relating to whether it's possible to rewrite the body of Func2() so that this code compiles without error. I realize that it is possible to use template specialization here, for example, but that doesn't meet the criteria of the question. Thanks!