You basically want something different than what standard C++ provides. Notice that language standards do not define what optimizations are required, but only most of what optimizations are legitimate.
CyghusX1 answer give some general hints.
If you are on POSIX (e.g. Linux), you could also generate some specialized C++ code at "runtime", fork a compilation of it, and use dynamic loading facilities (i.e. dlopen
) on that generated "plugin"; to be more specific:
- have some model of the AST of the subset of C++ you want to generate
- compile and link your main program with
-rdynamic -ldl
- define some conventions regarding how to call your plugin, e.g. deciding that you want some
extern "C" int foo(const char*)
function in it.
- write some code declaring types for that AST
- write the code emitting that AST as C++ code in some file, the generated C++ code should follow your plugin convention (so would provide the
int foo(const char*)
function you want; avoid name mangling)
- concretely, emit the C++ code in a temporary file
/tmp/temp1234.cc
- fork its compilation as a shared object by running something like
g++ -O -Wall -fPIC /tmp/temp1234.cc -shared -o /tmp/temp1234.so
, e.g. with system(3) or something else
- call dlopen(3) on
/tmp/temp1234.so
with RTLD_NOW|RTLD_GLOBAL
; check against failure (otherwise, use dlerror()
)
- call
dlsym
on the handle provided by dlopen
with the foo
external name. This gives a function pointer, which you can call later.
On Linux, you could repeat this many times (practically, many hundred thousands times at least, see my manydl.c example). My GCC MELT tool is doing such things.
Actually, GCC MELT is for customizing and extending the GCC compiler, and you could perhaps even use for your goals, since it enable customization of GCC; so you might add some new attributes or pragmas marking the functions you would want to specialize and code the MELT extensions doing that specialization (you'll need to understand GCC internals, notably Gimple, so that would take you more than a week, and your __COMPACT_PRETTY_FUNCTION__
could use some of your additional GCC builtin provided by your MELT extension).
So have something like
#define __COMPACT_PRETTY_FUNCTION__ _builtin_compact_pretty_function()
and code the MELT extension implementing your new _builtin_compact_pretty_function
compiler builtin.