r/cpp • u/MindsAndMachines • 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:
creates a new function (in the source code):
double final_func() { return sum(minus(5, 6), minus(3, 4)); }
compiles the binary with only the functions below:
sum; minus; final_func; main
The naive approach is to:
write a parsing app that
parses cfg to generate the source code for
final_funcset some flags and use macros to only include relevant functions, e.g.
sum; minussave the modified source code to a temp location,
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.
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_funcas 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.
4
u/MysticTheMeeM Aug 19 '24 edited Aug 19 '24
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).
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:
With the +0 meaning that, even if your config is empty, your function is well defined (unary positive 0 vs some value plus 0).