C ++`operator function_type()`有可能吗?

时间:2011-12-03 15:55:24

标签: c++ compiler-errors operator-overloading

我尝试编译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

3 个答案:

答案 0 :(得分:2)

您可以将转换运算符用于函数指针类型,但语法不允许您直接指定函数类型。你需要做的就是使用一个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 *。

答案 1 :(得分:2)

包含函数指针的声明令人费解,因此您可能首先尝试使用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];}

你也有constness的问题。地图的operator[]是一项修改操作,因此您必须改为使用map::find,或者也可以创建自己的operator[]非const。


关于如何传递参数也很不清楚。例如,为什么将const指针传递给字符串而不是const引用?为什么你有函数按值传递字符串和通过const引用传递int(后者特别没有意义,因为int的复制成本更低)。

答案 2 :(得分:1)

由于我为你编写了原始代码,我觉得有必要解决它:(

#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