ここでの問題は、関数ポインターにマップできないことではなく、その逆です。
現在のセットアップでは、文字列を介してクラスをインスタンス化できます。今、クラス型から文字列を取得しようとしています。
私の提案した方法:
class A {};
template <typename T> T* create(void) { return new T; }
static std::map<std::string,A*(*)(void)> str_to_class;
static std::map<A*(*)(void),std::string> class_to_str;
template <typename T> void Bind(std::string identity) {
// T must inherit from A.
str_to_class[identity]=&create<T>;
class_to_str[&create<T>]=identity;
}
A* MakeFromString(std::string identity) {
return str_to_class[identity](); // Compiles fine.
}
template <typename T> std::string GetTypeString(void) {
return class_to_str[&create<T>]; // Error!
}
int main(int,char**) {
Bind<A>("A");
A* new_entity=CreateFromString("A");
}
Error: C2679: binary '[' : no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)
dynamic_cast<> を使用してエンティティ タイプをチェックできることはわかっていますが、それには、使用されるすべてのクラスのコードを記述する必要があります。