r/cpp • • 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.html

Note: 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);

}

}; 

}

  1. 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

49 Upvotes

87 comments sorted by

View all comments

Show parent comment

3

u/RoyAwesome Sep 25 '26 edited Sep 26 '26

This is exactly the problem i'm hoping to solve.

How do we reflect across TUs?

I can get this kind of working with CMake. Run a compile, do some shenanigans to output some registration stuff into a .cpp file, include that in the build process, and then link that in together as well.

Only issue is that it's Cmake specific! I would like the ability to "see" across TUs to write boilerplate registration code or whatnot for the whole program.

A good user story for this "As a developer, I want to be able to write a function that looks at every type in my program that contains a [[+my::property(scriptable)]] annotation and generate a function that binds that property to a scripting language". This is not possible on a large project, and still needs external programs to pull this off.

3

u/daveedvdv EDG front end dev, WG21 DG Sep 26 '26

We've been talking about I/O during constant evaluation. There are security concerns there, but you could imagine having each compilation write to an adjunct .cpp file that your build system would then pick up.

2

u/13steinj Sep 26 '26

On one hand I/O would be very cool!

On the other hand I severely question how useful such a thing would be in practice, and don't want to promote bad patterns by people who are too smart for their own good.

2

u/RoyAwesome Sep 27 '26

Yeah, that's janky though. I would prefer this to be a language feature.

1

u/TSP-FriendlyFire Sep 26 '26

I think the first half of the feature you and I want is addressed within P4380R0 already in section 2.5 with the concept of annotation hooks.

That'd let us run code automatically for each annotated type. All we'd need would be a way to persist some kind of data in a centralized location from there. Maybe some kind of consteval-only global TU, maybe some addition to the linker, I'm not sure.

1

u/RoyAwesome Sep 26 '26

Some kind of hook could work here yeah. The question becomes where to write it to. I see where you are going with this.

Possible solution here if this is expanded!

1

u/13steinj Sep 26 '26

Does the standard understand the concept of a linker? Or does it just pretend as if your C++ program is one TU?

1

u/RoyAwesome Sep 26 '26 edited Sep 26 '26

I don't think it does. linktime constexpr might be a solution here, but who knows what people can come up with to solve this problem! I'm open to whatever, so long as I can do a pass over all types in my program regardless of what TU they show up in.

EDIT: /u/TSP-FriendlyFire came up with a good idea to do injection hooks that run at the end of the compile process! I'm not opposed to that we just need to be able to write the code somewhere.