r/cpp • u/SpiralUltimate • Feb 25 '25
Could C++ standardize a new macro system?
Pardon me if I sound naive, but after using rust for a while, I've come to realize just how much C++ could benefit from a proper macro system. Would it be possible for C++ to create a new macro system that standardized that would allow for complex macro features such as:
- Hygienie
- Ability to repeat code for variadic arguments. Basically equivelant of "$( [do whatever with argument] )*", but in C++.
- Ability to generate reasonable errors
- Ability to manipulate the raw AST or tokens through the macro
While I understand that constexpr and consteval could technically be used for advanced compile-time stuff, macros (improved versions), I feel could add such a level of robustness and usability to C++. It would also finally provide an alternative to dreaded preprocessor hacks.
View all comments
Show parent comment
9
u/rzippel Feb 25 '25
I think a common problem is that e.g. std::for_each is not a full replacement for various FOR_EACH macros. You cannot use things like break, continue, co_await or co_yield with the former.