C++ C++`运算符函数_type()`这是可能的吗?
我试图编译我竞争(在我看来)提供的答案代码:C++ C++`运算符函数_type()`这是可能的吗?,c++,compiler-errors,operator-overloading,C++,Compiler Errors,Operator Overloading,我试图编译我竞争(在我看来)提供的答案代码: #include <functional> #include <iostream> #include <string> #include <map> class api { //maps containing the different function pointers std::map<std::string, void(*)()> voida; std::ma
#include <functional>
#include <iostream>
#include <string>
#include <map>
class api {
//maps containing the different function pointers
std::map<std::string, void(*)()> voida;
std::map<std::string, int(*)(std::string, const int&)> stringcrint;
friend class apitemp;
public:
//api temp class
//given an api and a name, it converts to a function pointer
//depending on parameters used
class apitemp {
const std::string* n;
api* p;
public:
apitemp(const std::string* name, const api* parent)
: n(name), p(parent) {}
operator void(*)()()
{return p->void[*n];}
operator int(*)(std::string, const int&)()
{return p->stringcrint[*n];}
};
//insertion of new functions into appropriate maps
void insert(std::string name, void(*ptr)())
{voida[name]=ptr;}
void insert(std::string name, int(*ptr)(std::string, const int&))
{stringcrint[name]=ptr;}
//operator[] for the name gets halfway to the right function
apitemp operator[](std::string n) const {return apitemp(n, this);}
} myMap;
int hello_world(std::string name, const int & number )
{
name += "!";
std::cout << "Hello, " << name << std::endl;
return number;
}
int main() {
myMap.insert("my_method_hello", &hello_world );
// int a = myMap["my_method_hello"]("Tim", 25);
}
所以我想知道-如何使它编译
更新:修复后(感谢我),我们得到了:
#include <boost/function.hpp>
#include <iostream>
#include <string>
#include <map>
template <typename T> struct identity { typedef T type; };
class api {
//maps containing the different function pointers
std::map<std::string, identity<void(*)()>::type > voida;
std::map<std::string, identity<int(*)(std::string, const int&)>::type > stringcrint;
friend class apitemp;
public:
//api temp class
//given an api and a name, it converts to a function pointer
//depending on parameters used
class apitemp {
std::string* n;
api* p;
public:
apitemp(std::string* name, api* parent)
: n(name), p(parent) {}
operator identity<void(*)()>::type()
{return p->voida[*n];}
operator identity<int(std::string, const int&)>::type*()
{return p->stringcrint[*n];}
};
//insertion of new functions into appropriate maps
void insert(std::string name, void(*ptr)())
{voida[name]=ptr;}
void insert(std::string name, int(*ptr)(std::string, const int&))
{stringcrint[name]=ptr;}
//operator[] for the name gets halfway to the right function
apitemp operator[](std::string n) {return apitemp(n, this);}
} myMap;
int hello_world(std::string name, const int & number )
{
name += "!";
std::cout << "Hello, " << name << std::endl;
return number;
}
int main() {
myMap.insert("my_method_hello", &hello_world );
int a = myMap["my_method_hello"]("Tim", 25);
}
可以将转换运算符转换为函数指针类型,但语法不允许直接指定函数类型。您需要做的就是使用typedef,我在这里将其包装在一个模板中:
template <typename T> struct identity { typedef T type; };
...
class api {
// You can use identity<F*>::type
operator identity<void(*)()>::type();
// or you can use identity<F>::type*
operator identity<int(std::string, const int&)>::type*();
};
模板结构标识{typedef T type;};
...
类api{
//您可以使用identity::type
运算符标识::type();
//也可以使用identity::type*
运算符标识::类型*();
};
该代码还有几个其他错误,例如使用常量api*初始化api*并在需要std::string*的位置传递std::string。可以使用转换运算符转换函数指针类型,但语法不允许直接指定函数类型。您需要做的就是使用typedef,我在这里将其包装在一个模板中:
template <typename T> struct identity { typedef T type; };
...
class api {
// You can use identity<F*>::type
operator identity<void(*)()>::type();
// or you can use identity<F>::type*
operator identity<int(std::string, const int&)>::type*();
};
模板结构标识{typedef T type;};
...
类api{
//您可以使用identity::type
运算符标识::type();
//也可以使用identity::type*
运算符标识::类型*();
};
该代码还有其他几个错误,例如使用常量api*初始化api*并在需要std::string*的位置传递std::string。包含函数指针的声明令人费解,因此您可以先尝试使用typedef
typedef void (* no_arg_fun)();
typedef int (* arg_fun)(std::string, const int&);
operator no_arg_fun()
{return p->voida[*n];}
operator arg_fun()
{return p->stringcrint[*n];}
你也有康斯特内斯的问题。Map的操作符[]
是一个修改操作,因此您要么使用Map::find
,要么使自己的操作符[]
也非常量
关于如何传递参数也存在不一致性。例如,为什么要将常量指针传递给字符串而不是常量引用?为什么函数按值传递字符串,按常量引用传递整数(后者尤其没有意义,因为复制整数更便宜)。包含函数指针的声明令人费解,因此您可以先尝试使用typedef
typedef void (* no_arg_fun)();
typedef int (* arg_fun)(std::string, const int&);
operator no_arg_fun()
{return p->voida[*n];}
operator arg_fun()
{return p->stringcrint[*n];}
你也有康斯特内斯的问题。Map的操作符[]
是一个修改操作,因此您要么使用Map::find
,要么使自己的操作符[]
也非常量
关于如何传递参数也存在不一致性。例如,为什么要将常量指针传递给字符串而不是常量引用?为什么函数按值传递字符串,按常量引用传递整数(后者尤其没有意义,因为复制整数更便宜)。既然我为您编写了组织代码,我觉得有义务修复它:(
#包括
#包括
#包括
#包括
类api{
//包含不同函数指针的映射
typedef void(*voidfuncptr)();
typedef int(*stringcrintptr)(标准::字符串,常量int&);
std::mapoidia;
std::map stringcrint;
公众:
//api临时类
//给定api和名称,它将转换为函数指针
//取决于使用的参数
类apitemp{
常量std::字符串n;
常数api*p;
公众:
apitemp(常量标准::字符串和名称,常量api*父级)
:n(名称),p(父项){}
运算符voidfuncptr()
{return p->voida.find(n)->second;}
运算符stringcrintptr()
{return p->stringcrint.find(n)->second;}
};
//将新函数插入到适当的映射中
void insert(const std::string&name,voidfuncptr ptr)
{voida[name]=ptr;}
无效插入(常量std::string&name,stringcrintptr ptr)
{stringcrint[name]=ptr;}
//名称的运算符[]到达右函数的一半
apitemp运算符[](std::string n)常量{return apitemp(n,this);}
}myMap;
int hello_world(std::string name、const int和number)
{
姓名+=“!”;
std::cout既然我为您编写了组织代码,我觉得有义务修复它:(
#包括
#包括
#包括
#包括
类api{
//包含不同函数指针的映射
typedef void(*voidfuncptr)();
typedef int(*stringcrintptr)(标准::字符串,常量int&);
std::mapoidia;
std::map stringcrint;
公众:
//api临时类
//给定api和名称,它将转换为函数指针
//取决于使用的参数
类apitemp{
常量std::字符串n;
常数api*p;
公众:
apitemp(常量标准::字符串和名称,常量api*父级)
:n(名称),p(父项){}
运算符voidfuncptr()
{return p->voida.find(n)->second;}
运算符stringcrintptr()
{return p->stringcrint.find(n)->second;}
};
//将新函数插入到适当的映射中
void insert(const std::string&name,voidfuncptr ptr)
{voida[name]=ptr;}
无效插入(常量std::string&name,stringcrintptr ptr)
{stringcrint[name]=ptr;}
//名称的运算符[]到达右函数的一半
apitemp运算符[](std::string n)常量{return apitemp(n,this);}
}myMap;
int hello_world(std::string name、const int和number)
{
姓名+=“!”;
std::cout抱歉,但我认为您应该在编译器的帮助下,而不是在我们的帮助下,来处理语法错误。您的代码中的错误太多了。慢慢添加代码并经常进行编译会更容易。apitemp需要一个std::string*但您需要传递一个std::string。抱歉,但我认为您应该在编译器的帮助下,而不是在我们的帮助下处理语法错误s、 代码中的错误太多了。慢慢添加代码并频繁编译会使它更容易。apitemp接受std::string*但传递std::string。
#include <functional>
#include <iostream>
#include <string>
#include <map>
class api {
//maps containing the different function pointers
typedef void(*voidfuncptr)();
typedef int(*stringcrintptr)(std::string, const int&);
std::map<std::string, voidfuncptr> voida;
std::map<std::string, stringcrintptr> stringcrint;
public:
//api temp class
//given an api and a name, it converts to a function pointer
//depending on parameters used
class apitemp {
const std::string n;
const api* p;
public:
apitemp(const std::string& name, const api* parent)
: n(name), p(parent) {}
operator voidfuncptr()
{return p->voida.find(n)->second;}
operator stringcrintptr()
{return p->stringcrint.find(n)->second;}
};
//insertion of new functions into appropriate maps
void insert(const std::string& name, voidfuncptr ptr)
{voida[name]=ptr;}
void insert(const std::string& name, stringcrintptr ptr)
{stringcrint[name]=ptr;}
//operator[] for the name gets halfway to the right function
apitemp operator[](std::string n) const {return apitemp(n, this);}
} myMap;
int hello_world(std::string name, const int & number )
{
name += "!";
std::cout << "Hello, " << name << std::endl;
return number;
}
int main() {
myMap.insert("my_method_hello", &hello_world );
int a = myMap["my_method_hello"]("Tim", 25);
}