-3

Hi I'm new to c++ and I want to write a program witch allows me to test different Functions witch are defined in it when it is compiled and running.

I could do a simple switch that receives the case from a cin but then I would have to maintain that for every function i write, plus I'm not sure how to pass on the arguments through that (like I said I'm new)

I come from haskell in wich you can just call whatever funktion you like with custome parameters...(makes testing edge cases and single "Parts" really easy)

How would I do that in c++?

Thx

Johnny Bones
  • 8,786
  • 7
  • 52
  • 117
majorTom
  • 57
  • 6
  • 2
    A good book for beginners is the answer. – DeiDei Oct 01 '17 at 13:42
  • i just want a way to test my functions with different casses without compiling every time – majorTom Oct 01 '17 at 13:44
  • 1
    C++ is a compiled language, there is no way to execute it without compilation. As for testing, there are dedicated unit test suites that allows you to run only required parts on demand, such as Microsoft Unit Testing Framework for C++ or Boost.Test. – user7860670 Oct 01 '17 at 13:46
  • so there is no way to write something that can just call functions by name with arguments while running? – majorTom Oct 01 '17 at 13:53
  • 1
    You can definitely implement such program behavior and voila, you've got yet another dynamic language. – user7860670 Oct 01 '17 at 14:05
  • ok yeah i didn't think this through, the names of the functions dont even exsist anymore, after compiling only their Logic with all nessesary information remains since its supposed to be efficient. sorry dumm question – majorTom Oct 01 '17 at 14:17
  • Is your program free software available on http://github.com/ ? If you ask me (by email to `basile` at `starynkevitch` dot `net` mentioning the URL of your question) I might have a look and give you a few hints.... – Basile Starynkevitch Oct 01 '17 at 15:18

2 Answers2

0

The correct Answer for myself would be:

I can't do it because the names of the functions dont even exsist anymore. After compiling only their Logic with all nessesary information remains.

--> so you can't call functions by their names in compiled Code unless you want to write your own User Interface / keyword interpreter

thx

majorTom
  • 57
  • 6
  • The fact that C++ function names have disappeared at runtime should not forbid you to store some names *explicitly* in your program (see mention of `std::map` in my answer). Of course your names are *a priori* unrelated to those that the C++ compiler processes. – Basile Starynkevitch Oct 01 '17 at 15:10
0

A possible approach might be to embed some interpreter (like Lua, Guile, etc...) in your C++ application. Then your advanced user would code some script in that language and can call whatever routine you have embedded into (i.e. interfaced or glued with) that interpreter.

(alternatively, write your own interpreter, but that is a lot more work)

Another approach might be to use dynamic loading facilities, but these are operating system specific. On Linux (and POSIX) you would use dlopen(3) and dlsym(3). Be aware of name mangling, so use extern "C" for those C++ functions you want to load at runtime with dlsym (see C++ dlopen mini howto).

A variant could be to generate (with your C++ code) dynamically some C++ source in a temporary file (perhaps from some user input; you want some intermediate AST), fork a compilation of that code into a temporary plugin, and dynamically load that with dlopen, then run it (using raw function pointers obtained with dlsym)

But (in contrast to Haskell or Scheme or Ocaml) there is no REPL in C++, so you need to make one (using the tricks I considered above) if you want some, and that is a difficult task for a C++ newbie. C++ is practically a programming language which should be compiled (practically with a compiler such as GCC or Clang started by some build automation tool like GNU make or ninja). And C++ is a very complex language, difficult to learn and to compile, so all its compilers are rather slow.

BTW, be sure to learn at least C++11.

Perhaps you want some unit testing. There are tools and libraries to help on that (see this answer, and also that list)

I could do a simple switch that recieves the case from a cin

You should consider more fancy stuff. For example, you could at least have some std::map with std::string-s keys and values of some common std::function type then use lambda expressions to fill it.

You could read the latest Dragon Book; it is helpful to learn parsing, interpreting and compiling techniques from it (and they are more relevant to your wish than what you might imagine).

Read also SICP (freely downloadable introduction to programming, using Scheme); it is not about C++ but will help you to relate Haskell with C++ and will improve your thinking about programming.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • no I want to learn c++ not lua or something else and this question just ruined my small rep so great but thx still – majorTom Oct 01 '17 at 14:15
  • You also need to learn programming (and not just C++ syntax); see http://norvig.com/21-days.html for a useful insight. – Basile Starynkevitch Oct 01 '17 at 14:39
  • thank you for your awesomly complicated and long answer I'm affraid i won't be able to try any of this with my skill level, but i don't really want to force c++ to behave like this anyway, i just made the false asumtion that i would be able to approach it similar to haskell wich is dumm – majorTom Oct 01 '17 at 14:57
  • But your initial wish was not unwise, it is just much more complex in C++ than what you believe. – Basile Starynkevitch Oct 01 '17 at 15:07