0

How can i return a function object based on a string literal argument?

This argument need not be a template, but I want to be able to input a string literal and return a function object

Im looking for something like:

template<const char * s> func_pointer(int arg);

I have tried returning a lambda. I have also tried returning a class operator() and a templated function. None work.

Any help is appreciated!

EDIT: calling the function with an extern variable will not work for my purposes either.

EDIT: I am writing a language parser. I want to build framework for the parser to avoid rewriting a lot of code. Therefore, I want to be able to have a bunch of functions that parse the text. I want to have a base function that parses a single string literal (the program is lexed.), and I want to be able to pass in the string for each possible base function.

EDIT: Note that any function object that can convert a lambda and a function pointer to a generalized type(like std::function) will do.

Riolku
  • 572
  • 1
  • 4
  • 10
  • Hi, you can't use string literals as a non-type template parameter – Carl Jul 08 '18 at 03:15
  • @Carl is there no other way to do this then? I am aware that string literals are an invalid non-type template parameter. – Riolku Jul 08 '18 at 03:17
  • Does it need to be a string? It seems strange. Perhaps this is not the actual problem you're trying to solve. – DeiDei Jul 08 '18 at 03:19
  • @DeiDei I would like for it to be a string. I am trying to return a function that I can call, but I need to be able to manipulate the function procedure at compile-time by passing a literal. – Riolku Jul 08 '18 at 03:20
  • Sounds like the job for an `enum`. – DeiDei Jul 08 '18 at 03:21
  • @DeiDei I have little experience with enums, can you please expand? – Riolku Jul 08 '18 at 03:23
  • Related [question](https://stackoverflow.com/questions/51195445/is-it-possible-in-modern-c-to-pass-a-string-literal-as-a-parameter-to-a-c-te) – 1201ProgramAlarm Jul 08 '18 at 03:23
  • @1201ProgramAlarm that's lovely for the future. However, as of now, how does this help me for the current versions of c++? – Riolku Jul 08 '18 at 03:31
  • You can't. C++ does not work this way. You'll either need to explain the real problem you're trying to solve (no, not the one about using string literals as function pointers, but whatever problem you think this is the solution to), or you'll have to accept the fact that this cannot be done in C++. – Sam Varshavchik Jul 08 '18 at 03:40

2 Answers2

1

For me one of the easiest ways to map a string to a function is a map<string, function<int(int)>. Here's a simple example:

#include <string>
#include <iostream>
#include <functional>
#include <map>

int add(int a, int b){return a+b;}
int subtract(int a, int b){return a-b;}
int multiply(int a, int b){return a*b;}
int divide(int a, int b){return (b!=0?a/b: -1);}
map<string, function<int(int,int)>> functions = 
{
    {"add",add},
    {"subtract",subtract},
    {"multiply",multiply},
    {"divide",divide}
};

int main(int argc, char** argv)
{
    cout << functions["add"](5,2);
    return 0;
}
tinstaafl
  • 6,908
  • 2
  • 15
  • 22
  • this involves me rewriting the template. However, your usage of std::function allowed me to figure out how to do it, so thanks to you! – Riolku Jul 08 '18 at 14:08
0

What I was looking for is the std::function object ( thanks to @tinstaafl ), this way I can have lambdas and function pointers convertible to te same type. A function can return a lambda based on a string literal.

Riolku
  • 572
  • 1
  • 4
  • 10