c++的' operator function_type() '是可能的
C++ `operator function_type()` is such thing possible?
我尝试编译与如何在容器中存储具有不同签名的功能对象相关的答案(例如: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的回答)我得到了这个:
#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。
包含指向函数指针的声明令人费解,因此您可以先尝试使用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的operator[]
是一个修改操作,所以你要么用map::find
代替,要么把你自己的operator[]
也改成非const。
也不清楚如何传递参数。例如,为什么传递const指针到字符串而不是const引用?为什么有些函数通过值传递字符串,而通过const引用传递整型(后者尤其没有意义,因为复制整型更便宜)。
既然我为你写了区域代码,我觉得有义务修复它:(
#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);
}
http://ideone.com/SXAPu 相关文章:
- "error: no matching function for call to"构造函数错误
- 调用专用模板时出错"no matching function for call to [...]"
- 库函数需要一个 std::function<void(void)>,如何传入类函数?
- Confusion: decltype vs std::function
- 为什么 std::optional::operator=(U&&) 要求你是非标量类型?
- 为什么 std::function 可以作为 std::not2 的参数?
- 'max'匹配'std::function<const int &(const int &, const int &)>'无过载
- 'operator='已弃用:改用 QDir::setPath()
- 传递给std::function template的template参数究竟代表什么
- 过载'operator new'如何导致无限循环?
- 将带有unique_ptr的可变 lambda 传递给 const&std::function
- 绑定派生类方法C++从实例范围之外的分隔 std::function 变量调用
- "no matching function for call to 'Vector::Vector'"错误
- 如何在向量中删除 std::function<void()>?
- 编译时二叉搜索树错误的反向迭代器表示"no matching function call for operator=()"
- 从 std::ostream 重载 << 运算符时,为什么编译器会给出"too many parameters for this operator function"错误?
- 为什么需要"std::function::operator=(F &&)"来制作临时的"std::function"?
- std::function operator() 和 std::forward 中发生了什么?
- C++ std::function operator=
- 如何访问通过引用传递给operator= function的对象的私有数据?