I have this piece of code:
#include <iostream>
template<class T> void* ToPtr(T t) { return ToPtr((void*)t); }
void* ToPtr(void* i) { return i; }
void* ToPtr(int i) { return (void*)(long)(unsigned int)i; }
template<class T> int ToInt(T t) { return ToInt((void*)t); }
int ToInt(void* i) { return (int)(unsigned int)(long)i; }
int ToInt(int i) { return i; }
struct MyClass {
template<class T>
void* Find(T t) { return ToPtr(t); }
template<class T>
int FindInt(T t) { return ToInt(t); }
};
int main() {
MyClass myClass;
int myInt = 1;
std::cout << &myClass << std::endl;
std::cout << myInt << std::endl;
std::cout << myClass.Find(&myClass) << std::endl;
std::cout << myClass.Find(myInt) << std::endl;
std::cout << myClass.FindInt(&myClass) << std::endl;
std::cout << myClass.FindInt(myInt) << std::endl;
}
The program crashes in the first call to Find() but I'm not sure why. I'm using GCC 6.2.0, which is only C++14 compliant, otherwise I'd use constexpr. What am I doing wrong?
ToPtrwill not be able to find the functions declared after it. You are calling the template version recursively to infinity.