3

Is there any way a C++ beginner can implement something like this? For example:

./timerprogram sortalgorithm.cpp

where timerprogram.cpp at some point does something like argv[1](); to run the function whose name is given by the command-line argument?

Assuming that sortalgorithm.cpp was self-contained and had an array to sort already. I don't need the timing part, just how to call as a function a command-line argument. Is there anything build-in to C++ that will allow me to do this?

Austin
  • 5,355
  • 4
  • 40
  • 106
  • 1
    C++ is typically compiled and linked, not interpreted. So, there will not be a typical mechanism to allow you to invoke a function when given the C++ source code. However, there are platform specific mechanisms to invoke dynamically linked objects. – jxh Feb 13 '16 at 03:03
  • Checking my interpretation of your question: You want your program to execute code from another source file, as in compile this other program and then run it? – user4581301 Feb 13 '16 at 03:03
  • I may have not fully thought this through because I'm using an IDE where I just click run.. I guess I'd want it to create a header for the other script and then run basically a self-titled function in that script. Should I edit and be more specific or is this comment understandable? – Austin Feb 13 '16 at 03:06
  • after compilation in release mode function names and variable names are generally disappeared, only addresses. You can't call a `cpp` file anyway – phuclv Feb 13 '16 at 03:07
  • It won't work that way. You have to create the dynamic linked object first, and then get your code to use it. – jxh Feb 13 '16 at 03:10
  • I'm not sure what "dynamic linked object" means so I'm guessing I'm way out of my depth and should rein in my goals for this program – Austin Feb 13 '16 at 03:13
  • So to be clear, you want C++ script, IOW something which reads C++ source code from a file, and then executes it? You should definitely edit the question to spell this out more clearly. – hyde Feb 13 '16 at 09:00
  • @hyde well I'd want it to import a function and call it, so I guess that's the same. – Austin Feb 13 '16 at 16:51
  • Well, you can load a dynamic library (compiled code, not source) and call functions in it. This is usually called using plugins. – hyde Feb 13 '16 at 23:24

2 Answers2

2

Function names are used mostly by the compiler, to compile the code, and figure out when something calls a function "where" it actually is. Also by the linker too, but that's beside the point.

Although some C++ implementations might provide run-time extensions or libraries that can be used to resolve an address given its symbol name, the easiest and the most portable solution is for your program to simply have an array of strings, with your function names, and a pointer to the corresponding function.

Then, your main() searches the array for the requested function name, and invokes it via its function pointer.

How to implement this simple solution is going to be your homework assignment.

Sam Varshavchik
  • 84,126
  • 5
  • 57
  • 106
  • But that's not really what I'm looking for, because main() would need the name of the function already. If I wanted to edit main every time I test a new function, I might as well just paste the new function in the same script as main. – Austin Feb 13 '16 at 03:00
  • Awww man. You aren't even going to point OP at REDACTED or throw a bone and suggest they try REDACTED? – user4581301 Feb 13 '16 at 03:01
  • I'm not sure what that is, but it sounds bad. Please no. – Austin Feb 13 '16 at 03:02
  • @user4581301 What are you talking about? For OP, this is probably what you are looking for: http://stackoverflow.com/questions/41453/how-can-i-add-reflection-to-a-c-application. You're probably better off just doing something like the answer above. – mclaassen Feb 13 '16 at 03:06
  • Ok that page is completely unintelligible to me so I guess I'm going to give up on this for now – Austin Feb 13 '16 at 03:15
  • 1
    Yes, this is certainly not something that happens by magic. But there are certain things that can be done to automate the task of compiling the list of functions to be invoked & their names. It shouldn't take, oh, more than half an hour to whip up a quick Perl script that automatically greps the source code, extracts the function names, and spews out a robo-generated array declaration with everything. Then integrate it into my Makefile, and have everything built automatically by the make cycle. You do have to have scripting skills and knowledge, to do this. – Sam Varshavchik Feb 13 '16 at 03:24
2

No. The answer is no.

Most of the stuff you see about this are inside jokes.

There are silly ways to make it look like its working, but they are silly, and certainly not for beginners.

fluffybunny
  • 406
  • 3
  • 8
  • There are fairly simple ways to do this. So wether answer is yes or no, it depends on the definition of "simple". For example adding a tiny script to extract function names (probably marked with a special comment or macro), which generates the reflection data, and adding this to the build process, would be simple in my book. – hyde Feb 13 '16 at 07:38
  • I think if you followed the OPs comments you would see that extracting function names isn't going to do the trick; what he wants needs run-time compile or interpreter, and that's not easy. And to do do your solution or the interpreter would be an outright silly thing to do, at this point you should be using language designed for reflection instead of rolling your own goofy thing. – fluffybunny Feb 13 '16 at 08:45