0

Is there a way out to call a function directly from the what the user inputs ?

For example : If the user inputs greet the function named greet is called.

I don't want any cases or comparison for the call to generate.

#include <iostream>
#include<string>
using namespace std;

void nameOfTheFunction(); // prototype

int main() {
string nameOfTheFunction;

getline(cin,nameOfTheFunction);  // enter the name of Function

    string newString = nameOfTheFunction + "()"; // !!!

cout << newString;

// now call the function nameOfTheFunction

}

void nameOfTheFunction() {
cout << "hello";
}

And is there a concept of generating the function at run time ?

Suhail Gupta
  • 22,386
  • 64
  • 200
  • 328
  • Calling a specific function at runtime based on factors such as user input is the definition of a [Strategy pattern](http://en.wikipedia.org/wiki/Strategy_pattern) (just though I'd point that out). You still need to associate (map) specific strings with functions to do what you're wanting to do. – AusCBloke Dec 23 '11 at 07:40
  • @AusCBloke: hmm...I dont see how this is going to help the OP. By an overview of it it looks as simple as type erasure – Arunmu Dec 23 '11 at 09:14

3 Answers3

2

You mean run time function generation ??

NO.

But you can use a map if you already know which all strings a user might give as input (i.e you are limiting the inputs).

For the above you can probably use std::map &lt std::string, boost::function &lt... &gt &gt

Check boost::function HERE

Arunmu
  • 6,837
  • 1
  • 24
  • 46
  • You actually can generate brand new functions on the heap and execute them (or as you put it "run time function generation"). But the op is talking about invoking a pre-defined function by name. – Chris Eberle Dec 23 '11 at 07:23
  • @ Chris how can i generate new functions on the heap and execute them ? – Suhail Gupta Dec 23 '11 at 07:27
  • @Chris: if pre-defined functions needs to be invoked, then using the map is the way to go ..right ? I dont know about run-time function generation, I will google around and come back :) – Arunmu Dec 23 '11 at 09:04
  • @Chris: Can you point to some source where I can see how function is generated on heap at runtime ? – Arunmu Dec 23 '11 at 09:16
  • There's a book I read a while ago called "Beautiful Code" in which they talked about that idea a lot. The Windows 3.1 graphics layer used to do this. I don't know where to find more info on it, and frankly I know that many operating systems limit the ability to do this (they don't fully ban it though, it is useful). I'll see what I can dig up. – Chris Eberle Dec 23 '11 at 20:14
  • 1
    Basically the process is this: allocate some memory, make sure that the page is marked as executable (varies from platform to platform), and emit machine code to this memory. Then you can simply move the instruction pointer to the start of this code and bam -- you just generated a function dynamically. Naturally, this is below the C level, you'd need to understand your exact architecture quite well. And C itself would be unaware that this chunk of memory represents executable code -- so don't crash, that would be unpleasant. – Chris Eberle Dec 23 '11 at 20:21
  • As for the map, yes to me that seems like an acceptable solution. – Chris Eberle Dec 23 '11 at 20:39
  • @Chris: thanks for sharing the info. Idea seems cool, but a lot of work to do for that. – Arunmu Dec 24 '11 at 07:05
0

In short, no this isn't possible. Names in C++ get turned into memory offsets (addresses), and then the names are discarded**. At runtime C++ has no knowledge of the function or method names it's actually running.

** If debug symbols are compiled in, then the symbols are there, but impractical to get access to.

Chris Eberle
  • 47,994
  • 12
  • 82
  • 119
0

Generating a function at runtime has a lot of drawbacks (if it is possible at all) and there is generally no good reason to do it in a language like C++. You should leave that to scripting languages (like Perl or Python), many offer a eval() function that can interpret a string like script code and execute it.

If you really, really need to do have something like eval() in a compiled language such as C++, you have a few options:

  1. Define your own scripting language and write a parser/interpreter for it (lots of work)
  2. Define a very simple imperative or math language that can be easily parsed and evaluated using well-known design patterns (like Interpreter)
  3. Use an existing scripting language that can be easily integrated into your code through a library (example: Lua)
  4. Stuff the strings of code you want to execute at runtime through an external interpreter or compiler and execute them through the operating system or load them into your program using dlopen/LoadLibrary/etc.

(3.) is probably the easiest and best approach. If you want to keep external dependencies to a minimum or if you need direct access to functionality and state inside your main program, I suggest you should go for (2.) Note that you can have callbacks into your own code in that case, so calling native functions from the script is not a problem. See here for a tutorial

If you can opt for a language like Java or C#, there's also the option to use the compiler built into the runtime itself. Have a look here for how to do this in Java

onitake
  • 1,369
  • 7
  • 14