我尝试编译 this answer related to how to store functional objects with difrent signature in a container (eg: std::map) 我竞争(在我看来)提供了答案代码:
#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);
}
但是我在操作符的行上遇到了 12 个奇怪的错误:
Error 14 error C2665: 'api::apitemp::apitemp' : none of the 2 overloads could convert all the argument types Error 4 error C2586: incorrect user-defined conversion syntax : illegal indirections Error 8 error C2586: incorrect user-defined conversion syntax : illegal indirections Error 9 error C2440: 'initializing' : cannot convert from 'const api *' to 'api *' Error 10 error C2439: 'api::apitemp::p' : member could not be initialized Error 13 error C2232: '->api::stringcrint' : left operand has '' type, use '.' Error 2 error C2091: function returns function Error 3 error C2091: function returns function Error 6 error C2091: function returns function Error 7 error C2091: function returns function Error 11 error C2059: syntax error : '[' Error 1 error C2059: syntax error : '*' Error 5 error C2059: syntax error : '*' Error 12 error C2039: 'p' : is not a member of 'api'
所以我想知道 - 如何让它编译?
更新:修复后(感谢 hvd's answer)我得到了这个:
#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);
}
还有一个错误:
Error 1 error C2665: 'api::apitemp::apitemp' : none of the 2 overloads could convert all the argument types
最佳答案
您可以将转换运算符转换为函数指针类型,但语法不允许您直接指定函数类型。您需要做的就是使用 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*();
};
该代码还有其他几个错误,例如使用 const api* 初始化 api* 并在需要 std::string* 的地方传递 std::string。
关于C++ `operator function_type()` 这样的事情可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8368853/