I try to compile this answer related to how to store functional objects with difrent signature in a container (eg: std::map) I compeated (seems to me) provided answer code:
#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);
}
But I get 12 strange errors on lines with operators:
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'
So I wonder – how to make it compile?
Update: After fixes (thanks to hvd’s answer ) I we got this:
#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);
}
Yet one error stell stands:
Error 1 error C2665: 'api::apitemp::apitemp' : none of the 2 overloads could convert all the argument types
Since I wrote the orgional code for you I feel obliged to fix it 🙁
http://ideone.com/SXAPu