r/cpp • u/khaled-Ahmed-- • Sep 25 '26
Misleading Token Sequence Injection & Modern Macros: The Most Game-Changing Compile-Time Feature in C++29 (Try it on Compiler Explorer with Barry's Clang Compiler!)
https://open-std.org/JTC1/SC22/WG21/docs/papers/2026/p4380r0.htmlNote: This feature is targeted at the C++29 standard and is not actually included in the standard in the original language prepared for the publication. I didn't mention that it was a feature within the standard, and that was that. However, when I asked the AI to translate and organize the text, it made this incorrect edit to the title, thinking it was a good hoke. I apologize for this.
introduces Token Sequence Injection (std::meta::token_sequence) and Hygienic Language Macros (__macro) for C++29. It allows building, manipulating, and injecting C++ tokens at compile time with full type-awareness, context inspection, and zero preprocessor bugs!
. Token Sequence Injection (std::meta::token_sequence)
Instead of string manipulation or macro hacks, code is treated as a sequence of tokens stored in std::meta::token_sequence.
Acts as a random-access range (std::meta::size(seq), indexing seq[i] like array, concatenation +, += , == , =)
Features Token Interpolation (...) to splice expressions, variables, or types inside token literals ^^{ ... }.
Provides standard utilities like std::meta::id("arg", i) to generate unique identifiers, std::meta::tokenize to turns the string or array of chars to array of tokens with type std::meta::token_sequence, std::meta::stringize to turn array of tokens with type std::meta::token_sequence to string, and std::meta::queue_injection For indirect injection, such as injecting tokens into the current domain or a specific namespace.
Quick code example:
constexpr auto make_getter(std::meta::info member) -> std::meta::token_sequence {
auto name = std::meta::id("get_", name_of(member));
return ^^{
auto \(name)() const -> decltype(\(member)) {
return \(member);
}
};
}
- Modern Hygienic Macros (__macro)
Replacing #define, the new __macro feature runs at consteval time and returns a std::meta::token_sequence injected directly at the call-site:
Invoked with !: Called as macro!(...) , check!(a == b).
Receives Expression Reflections: Arguments are passed as expression handles (std::meta::info), preserving expression identity, source text, and value categories.
Prevents Double Evaluation: Injecting the same un-stored expression twice is a compile-time safety violation, eliminating bugs like MIN(x++, y).
Context-Aware (macro_expansion_context()): Can inspect the caller's scope (enclosing function, class, or return type) to validate syntactic and logical rules before injecting.
Module & Namespace Friendly: Fully scoped, exportable in modules, and can be class/namespace members.
Quick code example :
template <class T> __macro check(T&&expr) {
return ^^{
if (!(\(expr))) {
std::println("Check failed: {}", source_text_of(^^expr));
}
};
}
You can test the full implementation on Compiler Explorer today using Barry Revzin's Clang prototype fork!
Paper link: https://open-std.org/JTC1/SC22/WG21/docs/papers/2026/p4380r0.html
•
u/STL MSVC STL Dev Sep 25 '26
Moderator warning: Don't submit AI-generated posts. Yes, we can tell. I'm going to leave this one up because it's accumulating discussion.
18
u/Expert-Map-1126 vcpkg maintainer BillyONeal Sep 25 '26
I really do not like things that refer to themselves as being "in C++<number>" before they are in the WP.
17
u/samadadi Sep 25 '26 edited Sep 26 '26
The first thing that confused me was this "\()" syntax. Why not use "$()"? does "$" sign have a meaning in c++?
7
u/Qruk000 Sep 25 '26
Does it mean that we can finally abadon logging macroses like
#define LOG(...) if( log::level() >= log::current() ) log( __VA_ARGS__ );and migrate to modules propertly?13
u/RoyAwesome Sep 25 '26
The two things I want to see from reflection now that we have it in cpp26 is code generation and whole program reflection. This is an INCREDIBLE way to do code generation, and I'm very thankful for barry taking the lead on it and providing such an excellent proposal.
Now we just need whole program reflection. Link time constexpr maybe?
6
u/Resident_Educator251 Sep 25 '26
been using reflection in gxx 16.2 extensively; what's really missing are the actual code generating bits. The above is just another one of those need sooner then later features.
1
u/positivcheg Sep 25 '26
April fools today?
2
u/DangerousFront3866 Sep 25 '26
One question the paper was not clear if something like #[cache] can be done like in rust macros for functions. Ie is memoization possible with push annotations and it will work for recursive calls?
3
u/DangerousFront3866 Sep 25 '26
Just amazing. Read the new paper looks great
Well done
1
u/pdp10gumby Sep 26 '26
i can’t read this slop. Is there really a proposal to add metasyntactic hygienic macros in C++29? As an old Lisp developer, this was a basic feature I missed in C++ since around ‘88. Well, better late than never.
I did discuss it at one point with Stroustrup in the mid 90s, which ultimately ended up with us both thinking, “sadly, templates are the best we’re going to get, due to [issues that thankfully no longer apply]”
1
u/biowpn Sep 27 '26
Now THIS is something that C++ committee should spend time working on, instead of junk like profiles and contracts
0
u/j1xwnbsr Sep 26 '26
Well this is gonna make debugging squirrely race conditions even more fun....
-14
u/reini_urban Sep 26 '26
C++ is getting worse and worse. Yet another language just for matching in hygienic macros? We ve had readable and homoiconic hygienic macros for decades, we also have readable old-style macros already. it's a horror show, just reject it outright