So, to start, I'm hesitant to ask this because there is (basically) the same question regarding this on SO, but the answers didn't fix my problem.
Question I've checked: C++ map<char, static method pointer>? (Marked as duplicate of How to create class objects dynamically? but I'm not dynamically creating objects)
I'm working on a PEG parser (cpp-peglib), and would like to map a string (token rule name) to a static function (the parser function).
For those not familiar, cpp-peglib takes actions as lambdas, in the form of [](const SemanticValues& vs) {}
.
So I have a structure containing static methods that are my parser actions, looking something like this:
struct T {
static auto getParser_A() {
return [](const SemanticValues& vs) {/* return parsed token*/};
}
static auto getParser_B() {
return [](const SemanticValues& vs) {/* return parsed token*/};
}
};
I'd like to map the actions to the names of the rules, something like:
std::map<std::string,/* static method reference */> actionMap
So that I can add my rules like:
parser[RULE_NAME] = actionMap[RULE_NAME];
I have tried this:
map<string,function<T*>> mapping;
And this:
typedef T* (*action)();
map<string,action> mapping;
but I get could not convert ‘{{"A", T::getParser_A}, {"B", T::getParser_B}}’ from ‘’ to ‘std::map, std::function >’
for both versions.
What am I doing wrong? And how would store a static method returning a lambda in a map?