C++ 带有函数指针的映射
我这里的问题不是我不能映射到函数指针,而是更多的是相反 使用当前设置,我可以通过字符串实例化类。 现在,我试图从类类型中获取字符串 我建议的方法是:C++ 带有函数指针的映射,c++,map,function-pointers,C++,Map,Function Pointers,我这里的问题不是我不能映射到函数指针,而是更多的是相反 使用当前设置,我可以通过字符串实例化类。 现在,我试图从类类型中获取字符串 我建议的方法是: 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::st
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");
}
A类{};
模板T*创建(无效){返回新的T;}
静态std::将str_映射到_类;
静态std::将类_映射到_str;
模板无效绑定(标准::字符串标识){
//T必须从A继承。
str_to_class[identity]=&create;
class_to_str[&create]=标识;
}
A*MakeFromString(标准::字符串标识){
将str_返回到_类[identity]();//编译良好。
}
模板std::string GetTypeString(void){
将类_返回到_str[&create];//错误!
}
int main(int,char**){
约束(“A”);
A*new_entity=CreateFromString(“A”);
}
错误:C2679:二进制“[”:未找到接受“重载函数”类型的右操作数的运算符(或没有可接受的转换)
我知道我可以使用dynamic_cast检查实体类型,但这需要为将要使用的每个类编写代码。问题在于
create()< /代码>返回与指定为映射键模板参数的返回类型不同的类型。因为所有使用“代码> < <代码> >作为基础/主类类型,您应该考虑对<代码> CREATE()/<代码>进行相同的操作。
template A*create(void){return new T;}
^^^^
我做了类似的事情,将字符串映射到任何类型的函数指针。从我发布的答案来看:
#包括
#包括
#包括
#包括
int fun1(){
std::coutwhere是template create()
声明/定义的?哎哟,有“Construct”和“create”混合。修复。尝试为问题创建一个映射。您给出的代码是不完整的,当您填写不完整的部分时,它编译/工作正常…这很有意义,但它在将字符串映射的值分配给函数指针时不是做了同样的事情吗?是的,如果您绑定到从a
派生的类型,您将得到一个simil有关无效转换的ar错误。
template <typename T> A* create(void) { return new T; }
^^^^
#include <string>
#include <iostream>
#include <map>
#include <vector>
int fun1(){
std::cout<<"inside fun1\n";
return 2;
}
void fun2(void){
std::cout<<"inside fun2\n";
}
int fun3(int a){
std::cout<<"inside fun3\n";
return a;
}
std::vector<int> fun4(){
std::cout<<"inside fun4\n";
std::vector<int> v(4,100);
return v;
}
// every function pointer will be stored as this type
typedef void (*voidFunctionType)(void);
struct Interface{
std::map<std::string,voidFunctionType> m1;
template<typename T>
void insert(std::string s1, T f1){
m1.insert(std::make_pair(s1,(voidFunctionType)f1));
}
template<typename T,typename... Args>
T searchAndCall(std::string s1, Args&&... args){
auto mapIter = m1.find(s1);
/*chk if not end*/
auto mapVal = mapIter->second;
// auto typeCastedFun = reinterpret_cast<T(*)(Args ...)>(mapVal);
auto typeCastedFun = (T(*)(Args ...))(mapVal);
return typeCastedFun(std::forward<Args>(args)...);
}
};
int main(){
Interface a1;
a1.insert("fun1",fun1);
a1.insert("fun2",fun2);
a1.insert("fun3",fun3);
a1.insert("fun4",fun4);
int retVal = a1.searchAndCall<int>("fun3",2);
a1.searchAndCall<void>("fun2");
auto temp = a1.searchAndCall<std::vector<int>>("fun4");
return 0;
}