Whereas Warp*
can be implicitly converted to Object*
, a pointer to a function returning Warp*
cannot be implicitly converted to a pointer to a function returning Object*
. Nor, in general, can such a conversion be performed safely at all.
Now the reason why your code doesn't work should be clear. &S2M_CreateObject<Warp>
has type Warp* (*)(int, int)
, and this can't be implicitly converted to Object* (*)(int, int)
. Instead, you can make the S2M_CreateObject
function always return Object*
regardless of which type is actually created:
#include <map>
#include <string>
using namespace std;
struct Object {
Object() {}
Object(int x, int y) {}
};
struct Warp : Object {
Warp() {}
Warp(int x, int y) {}
};
template <class T>
Object* S2M_CreateObject(int x, int y) {
return new T(x, y);
}
int main() {
map<string, Object *(*)(int x, int y)> o {
{ "warp", &S2M_CreateObject<Warp> }
};
}