There are lots of features

int func001(); int func002(); ... int func015(); ... int func123(); 

How can all of them go through in a loop?
How to call a function knowing its number?

PS Removed not working code.

  • one
    I'm not sure that your code will work even with zeros. - KoVadim
  • 3
    Do you understand the essence of the preprocessor? It works BEFORE the compiler and replaces the occurrences of the specified lines, treating your program code as plain text. What you are going to iterate over the variable in the preprocessor loop is unknown. Your FUNC(i) it would funci() . - fori1ton
  • one
    And I about the same Will not earn. even if you correct the error in #define (already deleted. There the second ## is superfluous). - In principle, you can, of course, if you practice hunting and not in the preprocessor. Make .so from this code, load it with dlopen (), then generate symbolic names (sprintf () in a loop), get the address via dlsym () and call. - avp
  • one
    @Ildik Simpson, tell me where you get so many functions with similar names and why call them all? Maybe you can somehow do it in a human way? - fori1ton
  • one
    Another option. Even if they are static , but the executable module is not "striped." You can analyze your symbol table (in fact, popen(av[0], "r") output popen(av[0], "r") ) and call it when a suitable name is encountered. - avp

3 answers 3

@VladD , comments again ended, the previous one being deleted (well, that was copied), but the new one does not allow to create. I had to fill in the answer ...

It definitely needs to be solved by creating a shared library (.so or .dll in Windows).

Do this library. main, when launched, loads it and calls functions by name, depending on the task number.

-

Of course, you can generate the main source from the template every time.

But, IMHO with a loadable library (or several) and a configuration file for them describing the result arguments (assuming some development of the problem) is simply more beautiful.

    Create an array of pointers to your functions, and call functions when you walk through the array.

    But frankly, a very, very odd task for C ++.

    UPD

    Another option: write a Perl script that generates a large source file with a call to all these functions.

      Something like this.

       #include <map> void func1(); void func2(); int main() { std::map<int, func_t> funcs; funcs.insert(std::make_pair(1, &func1)); funcs.insert(std::make_pair(2, &func2)); for(std::map<int, func_t>::iterator i = funcs.begin(); i != funcs.end(); i++) { { i->second(/*аргументы*/); } } 
      • Yes, that's right, but most likely the forwarder is interested in how to “automate” the process and not write down all these functions with your hands) - AlexDenisov
      • Perfect solution! It's so cool - manually pushing all 123 functions in a map! - fori1ton
      • Well, the TS asked how to sort them out :) although, most likely, you are right. - Max Zhukov
      • @Max Zhukov, and how does your method (except for the number of letters typed) differ from the @ 1101_debian variant (array of functions)? - avp 1:51 pm
      • greater versatility apparently. As in it keys can be string. - KoVadim 2:21 pm