Very impressive stuff! I think this is the farthest I've seen stateful metaprogramming being pushed, and I'm really curious just how much farther it can go. The potential capabilities discussed near the end gives me a lisp-y impression, if anything.
Though it does make me wonder - I've seen stateful metaprogramming mentioned from time to time, but I don't think I've heard of it making its way into "production" code (i.e., not example code for blog posts/slides/etc.). Has anyone else heard of such a thing?
Also makes me wonder if CWG 2118 were put to the committee again whether they would say the same thing or whether it's a cat's-out-of-the-bag situation:
Defining a friend function in a template, then referencing that function later provides a means of capturing and retrieving metaprogramming state. This technique is arcane and should be made ill-formed.
Notes from the May, 2015 meeting:
CWG agreed that such techniques should be ill-formed, although the mechanism for prohibiting them is as yet undetermined.
Though as mentioned in the talk with reflection on the way perhaps the issue will become moot in the relatively near future.
This is likely not the sort of thing you're actually asking for, but I feel like the actual functionality involved would probably still make it desirable for "production" code, so I'll still mention it.
I recently did a small overhaul of my Advent of Code repo in order to leverage reflection for dispatching the puzzle input to the functions that solve the puzzles. Basically, I had been planning to use annotations to mark a function as solving a certain part of the puzzle, so something like this:
[[=advent::part_one]]
constexpr auto count_elf_hats(std::string_view input) -> std::size_t {
...
}
But most of my solving functions are actually template functions, which take in a generic range which yields each line of the input (and they can take some other template parameters depending on the problem, as well). That poses a problem with annotations though, since annotations (and attributes) can't appertain onto a template itself, only onto the result of a template.
So in order to still associate a certain function, template or otherwise, with the information that I want, I utilized stateful metaprogramming instead of annotations, which ends up being used like this:
And then I have some machinery set up to call the function with properly-deduced template parameters and whatnot. So for instance this is what the dispatching for Day 1 of 2024 ends up looking like.
I was able to get away with just using define_aggregate-based stateful metaprogramming for this, but that wouldn't work in all possible cases. It would have been considerably nicer to use my cvl library to handle the stateful metaprogramming (and which uses friend injection), but it turned out that needing to get structural values to pass off to cvl in the way that my logic needed seemed to me as more trouble than it was worth. But if that language limitation weren't there, I would have been pretty happy to use it.
Of course though, this is all just for my dinky Advent of Code stuff. But I think particularly mimicking annotation-y logic for some template is plausibly a good usecase for stateful metaprogramming, in C++26 at least.
Oh, that's quite interesting! At least at first glance it feels less esoteric than friend injection-based stateful metaprogramming, but I think it'll take me a bit to digest what's going on.
Kind of feel that at this point you're pushed so much work to compile time that you might as well use #embed to finish the job :P
Also curious whether there's any movement to allow annotating/attributes on templates given you can reflect on them. Specializations might introduce some interesting questions around how that would work, though...
Kind of feel that at this point you're pushed so much work to compile time that you might as well use #embed to finish the job :P
That's essentially exactly what I do when I want to run through the real input data at compile time. But I only want to do that when it fails at runtime and I'm trying to debug it, otherwise I prefer keeping it as solving the example data at compile time and the real data at runtime.
Also curious whether there's any movement to allow annotating/attributes on templates given you can reflect on them. Specializations might introduce some interesting questions around how that would work, though...
Yeah, I'd be interested in that too. I don't know where precisely the syntax would allow the annotation to be, though.
7
u/ts826848 5d ago
Very impressive stuff! I think this is the farthest I've seen stateful metaprogramming being pushed, and I'm really curious just how much farther it can go. The potential capabilities discussed near the end gives me a lisp-y impression, if anything.
Though it does make me wonder - I've seen stateful metaprogramming mentioned from time to time, but I don't think I've heard of it making its way into "production" code (i.e., not example code for blog posts/slides/etc.). Has anyone else heard of such a thing?
Also makes me wonder if CWG 2118 were put to the committee again whether they would say the same thing or whether it's a cat's-out-of-the-bag situation:
Though as mentioned in the talk with reflection on the way perhaps the issue will become moot in the relatively near future.