C++ `operator function_type()` 这样的事情可能吗?

标签 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

最佳答案

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

相关文章:

c++ - 从 .dcm 文件生成和读取 .vtk 文件,以及修改模型

c++ - node-gyp 和 contextify - 找不到 v140 的构建工具(平台工具集 = 'v140')

compiler-errors - 错误: environment variable 'STAGING_DIR' not defined

types - Ada:子类型转换或调用“Base”

c++ - 获取模板堆栈实现的 C++ 语法错误

c++ - 使用 omp_set_num_threads() 将线程数设置为 2,但 omp_get_num_threads() 返回 1

c - 警告 : conflicting types error

java - 循环体后找不到符号

c++ - 运算符 == 标记为 'override' ,但不覆盖

vector - 实现二维向量语法以访问一维向量?