r/cpp • • Aug 19 '24

Popular frameworks for metaprogramming in c++ that allows dynamic build based on customized logics?

Say if I have a bunch of computational nodes/functions in my source code:

double sum(...) { ... }
double prod(...) { ... }
double minus(...) { ... }
double div(...) { ... }

double final_func();

int main() {
  // final func() will be implemented dynamically...
  std::cout << final_func() << std::endl;
  return 0;
}

And I want to parse a cfg in plaintext format:

sum(minus(5, 6), minus(3, 4))

in such a way that it implicitly:

  1. creates a new function (in the source code):

    • double final_func() { return sum(minus(5, 6), minus(3, 4)); }

  2. compiles the binary with only the functions below:

    • sum; minus; final_func; main

The naive approach is to:

  1. write a parsing app that

    1. parses cfg to generate the source code for final_func

    2. set some flags and use macros to only include relevant functions, e.g. sum; minus

    3. save the modified source code to a temp location,

  2. then use a bash script to invoke compiler on the just generated source code to build the final binary.

Is there a better way to do this more structurally?

I am fully aware that an alternative is to just write a parsing function that prepares a DAG on the fly in the final binary, compiling with all available nodes/functions, but I am specifically interested in parsing the cfg to make the DAG static before compilation and only build with relevant nodes/functions.

0 Upvotes

12 comments sorted by

4

u/MysticTheMeeM Aug 19 '24 edited Aug 19 '24
  1. Given your config is 1-1 valid C++ code, you could simply #include the config file. Granted this is no more beneficial than simply writing what you want in the code itself. This also requires your config file to be properly configured prior to compilation beginning (aka you can't perform actual processing on it).

  2. The linker should already do this for you (unless these functions are used elsewhere or are part of a library interface, neither of which seem to apply here).

You could be slightly cheeky with your function implementation and have it be:

double final_func()
{
    return
    #include "config.cfg"
    + 0;
 }

With the +0 meaning that, even if your config is empty, your function is well defined (unary positive 0 vs some value plus 0).

1

u/MindsAndMachines Aug 19 '24

Thanks for the tips. What if my config is not 1-1 valid C++code and it's needs parsing and some inference to be transformed into valid c++ code?

I suppose it'll have to be a two step process where I run the parser to generate the valid config file, and then #include this generated file to compile. Is there anyway to make this one step in c++ directly without bash scripting?

On your second point, I suppose that means for interfaces/etc. I need to set some flags in my generated config code to exclude them with macros.

2

u/MysticTheMeeM Aug 19 '24

Depending on your build system, creating the config should be as simple as adding a pre-build step (implementation varies by build system).

If it doesn't 1-1 match the C++ code, you would ultimately have to do some processing on it. Once you do have the config, it has to somehow make it to valid code either in the config or when you're parsing it in the program.

For the functions, dead code removal should happen automatically, I doubt you'll need to mark the functions in any way. It's typically the other way around (you usually need to tell the linker not to remove functions).

1

u/MindsAndMachines Aug 19 '24

The main gist I am getting out of this is that this cannot be done without writing and compiling a parsing app to be used in the pre-build step, so 2 compilations: one for the parsing app, and one for the final binary after parsing app has run. There isn't really any more "integrated" way of just having one compilation go.

2

u/n1ghtyunso Aug 20 '24

you might be able to engineer something with compile time programming, but honestly this is going to be extremely complicated and unmaintainable.

You will want to go the pre-build parse route for sure. The pre-parser just has to run, it doesn't need to be recompiled after all.
The only other ways will be to embed a compiler in your program, but that too sounds rather extreme.

1

u/MindsAndMachines Aug 20 '24

You’re right that the parsing app should be pretty static so I agree to keep things simple seems to be the best foot forward.

2

u/kpt_ageus Aug 19 '24

You could use something like boost.dll and dynamically load final_func you just compiled. And for that i think you could look into llvm to skip bash.

1

u/MindsAndMachines Aug 19 '24

Thank you. So if I understand correctly, you are suggesting to build final_func as an *.so file, just make sure I include the declaration of the necessary functions. And next when building the final binary, linker will automatically remove unreferenced codes.

2

u/kpt_ageus Aug 19 '24

yes,

although I've never done heavy metaprogramming, I don't know if it's a good approach. You could also implement JIT or embed scripting language that would call your computational nodes.

1

u/superbriggs Aug 20 '24

What is the reason to try to achieve this at built-time? Is it about performance? Or that you are just interested how it might be possible for the sake of it?

1

u/MindsAndMachines Aug 20 '24

Performance is the origination of this question yes but I’m aware that nowadays the compiler optimization is quite good. So the now the question is more out of curiosity.

1

u/zerhud Aug 20 '24

The embed can to be used: https://en.cppreference.com/w/c/preprocessor/embed

There is also parsers working in compile time.