LightSith
LightSith

Reputation: 927

C++ alternative of rust's proc macros

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

Answers (1)

Matthieu M.
Matthieu M.

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:

  • Code generation: a separate executable runs as part of the build to generate C++ files. For example LLVM features TableGen.
  • Preprocessing: a separate executable preprocesses C++ files and transforms them prior to the build process. For example QT features MOC.

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

Related Questions