Reputation: 927
Rust have very amazing feature to generate code at compile using proc_macros which are pure rust code. is there is anything similar in C++ which lets me executes real C++ code to generate code instead of ugly defines.
Upvotes: 5
Views: 2243
Reputation: 300149
There is nothing built into C++ to achieve the equivalent of Rust proc-macros, at the moment.
Traditionally, in C++, I have seen 2 different ways to achieve a similar effect:
I would argue that QT's MOC is closer in spirit to Rust proc-macros, and LLVM TableGen is closer to Rust's build.rs
.
Herb Sutter has been proposing meta-classes, which would cover a subset of the proc-macros: the derive macros.
It would not cover the use of proc-macros to transform the code of functions, or otherwise generate arbitrary code in-situ.
Upvotes: 7