This library is centered around the same basic concept that I showed in a previous post, but with I think a fair number of improvements from the proof-of-concept code which was shown there.
I had been sitting on this library for a bit now while I tried to hammer out a blogpost about it, where I'd discuss a couple interesting points of the implementation, but it just wasn't coming out like how I wanted and I was getting increasingly annoyed with writing it, so I decided just to post the repo here instead.
There I have to pull out each result of counter.count() into its own constexpr variable and then static_assert with that variable, instead of just doing static_assert(counter.count() == 0) and such, since otherwise the compiler appears to end up evaluating counter.count() multiple times, which increments the counter too far.
That would maybe be a clue that __COUNTER__ is the better fit there for more serious code. But you could have this cvl_counter be a function-local static so you wouldn't have to keep track of the initial value of __COUNTER__, but that seems a small benefit.
Maybe it could end up integrated with the language better somehow, if you avoid __COUNTER__, but I'm not completely sure. Would be interesting to see for sure, though.
cvl uses the reflection of the address of certain objects, like cvl::once_flag and such, as "tag" template arguments that can be substituted into some "backend" template that will result in an instantiation that is distinct from all the other backend instantiations. And that's needed so that there can be unique types for the friend injections that power the mutable state.
But using the addresses as those tags is nice because they can be automatically retrieved for a given declaration, and it allows every cvl::variable that represents an int to have the same type of cvl::variable<int>, instead of some cvl::variable<int, my_unique_tag> or whatever.
The relevant source file in cvl is here. Interestingly too, the error message from Clang changed from the last time I tried it. When I tried it before it seemed to get confused over consteval-only types when calling std::meta::reflect_constant(this).
Additionally, because I have to drop Clang support because of that, I decided to use some things that Clang just hasn't implemented, like std::meta::exception and std::meta::current_function, but those could be relatively easily skirted around if need be.
EDIT: If I disable exceptions and add some things under the std namespace, then this is the closest I can get to Clang support, which seems to have that consteval-only type error: https://godbolt.org/z/EKb55Tq8o
EDIT 2: Heh, I can get the initial link working if I instead use the pointer of the std::meta::info subobject instead of this directly, since I guess Clang is more assured that that has a consteval-only type: https://godbolt.org/z/f93sWjrjb. Maybe something to investigate, then.
EDIT 3: Even if I make changes to make it like the above edit, then Clang will still eventually get mad at me for reflecting on the address of a "temporary", even though since all these things are prvalues, they should never actually move and their addresses should be static. So maybe Clang's internals were just a bit iffy about that, but it was never an issue until exposing them to reflection in this way.
Stateful metaprogramming via friend injection? If so, nice! I'd like to see it normalized as a strange but powerful technique like CRTP and SFINAE. Currently, I don't feel safe enough to use it in production due to the fact that the standards committee hates stateful metaprogramming.
Yep, friend injections are what power the mutable state for cvl. Together, cvl::once_flag and cvl::delayed_init are abstractions over friend injection, which all the other mutable state features are built on top of.
Though, technically you could avoid even cvl::delayed_init and use a series of cvl::once_flags as the backing storage bits for some object, like I've done here in this snippet: https://godbolt.org/z/PqsGqYa1z
But that's getting a bit too cursed, even for me :P
One could also argue that `-fno-rtti` and `-fno-exceptions` create dialects too, though that's more fuzzy because they're syntactic and semantic subsets.
I wouldn't count "extern C", every language has some way to do that, which is effectively the only way to communicate with the OS (somewhat) directly. The C preprocessor which macros come from could very well be called a separate language, though more divorced from C++ than all metaprogramming is.
Just a few days ago I had a discussion with a friend about a certain problem when it comes to producing python bindings with reflections. The solution required one of two things:
P3294 token sequences
Or mutable consteval
You even made a container for this. I told my friend I won't get into stateful metaprogramming but cvl looks tempting.
To elaborate, what we were discussing was generating the "trampolines" for pybind11 bindings.
The short story is: python bindings need to support abstract classes, yet those are non-constructible on their own, so how could python hold a reference to the base object? The solution are the "trampoline" classes. See the pybind docs for examples. To be perfectly clear, these trampolines are 100% boilerplate and the only boilerplate that I couldn't get rid of with C++26.
C++26 can't really generate those, because define_aggregate can only produce simple aggregates, so my friend and I came up with this:
struct Base {
virtual void f() = 0;
};
struct
[[=trampoline_for(^^Base)]]
PyBase : Base { /* ... */ };
And then construct a consteval, mutable registry of what have we seen, so that when bind_class<T>(module) is called, you look through the registry and apply the trampoline.
Hmm, I'm not sure I understand where mutable consteval comes in. Couldn't you just look through the members_of some namespace (even the global one) and see which types have the trampoline_for annotation?
You could, but that ends up being O(n^2) with respect to the number of seen types:
Bind a new type
Scan the relevant namespace
Bind another type
Rescan the namespace
But if you have a mutable, consteval hash map whose lookup is O(1), then that becomes O(n), though needing two iterations - one to register all trampolines and another to actually make the bindings.
I did skip that detail, and a lot of other details, for brevity.
I see, yeah that could maybe be worth it, potentially. If you do pursue that, then maybe this could serve as some inspiration: https://godbolt.org/z/ebbj3czzT
That uses a cvl::delayed_init which is templated over the base type as the "backing storage" for some (conceptual) map between base types and their trampolines.
This usecase seemed compelling, so I ended up adding cvl::map. Its example is here. It operates essentially like what I linked in my other reply, but a bit more structured.
I also realized that a cvl::map is really just a lower-level cvl::list, which maps from a given index to its value, so I rebased cvl::list on top of cvl::map. And the code's ended up nicer, I think. So thanks
Your CMakeLists.txt doesn't have an install() and it's giving me troubles.
CMake Error in CMakeLists.txt:
install(EXPORT "pymetabind-targets" ...) includes target
"pymetabind.extension_module" which requires target "cvl" that is not in
any export set.
I can clone cvl in a subfolder and do target_include_directories(), but target_link_libraries(pymetabind INTERFACE cvl::cvl) gives me the above error.
Sorry about that. I'm not as adept with CMake as I'd like to be. I'm more used to meson. There should be an install target now, if you were still wanting that.
But anyways, that seems pretty cool, nice to have someone using this :)
One note I would add from skimming the commit, you don't have to return the map as a reference, you can return it by value. All the mutable-state-modeling objects are cheap to copy and have view-like/reference-like semantics, like pointer types and std::meta::info do. But if you find returning the reference to be cleaner, then by all means you can do that.
I know the feeling. Pymetabind was my motivation for actually learning cmake and how to be a good cmake librarian. What helped my understanding is patiently going through the official cmake tutorial. The first half was nothing new, but later chapters, about dependencies and install() taught me a lot.
The install() works now, but redirecting find_package(cvl) through FetchContent made my cmake hit an assert. I've hit compiler bugs and debugger bugs before, but this is the first time I hit a build system bug! :D Here's an old bug report that talks about the assertion that I've hit. https://gitlab.kitware.com/cmake/cmake/-/work_items/26622
you don't have to return the map as a reference, you can return it by value.
My thought was "this is a local static, so it should be returned by reference", just like you'd do with regular non-constexpr variables.
The install() works now, but redirecting find_package(cvl) through FetchContent made my cmake hit an assert. I've hit compiler bugs and debugger bugs before, but this is the first time I hit a build system bug! :D Here's an old bug report that talks about the assertion that I've hit. https://gitlab.kitware.com/cmake/cmake/-/work_items/26622
Hmm, seems to be from your use of the experimental EXPORT_PACKAGE_DEPENDENCIES thing. I'm not sure if there's anything for me to do on my end about that.
My thought was "this is a local static, so it should be returned by reference", just like you'd do with regular non-constexpr variables.
And yeah, that's what one would do with most objects, but because cvl::map is already reference-like, it's fine to copy it. Doesn't really matter either way, though.
Something that you could do though, which would maybe look a bit more natural, is to organize it like
Or similar. Plausibly you don't even need the interceding register_trampolines function, you could just do it all inline in the consteval block. But it's up to preference.
Basically, all this work was inspired by the end of Barry Revzin's blogpost "Behold the power of meta::substitute", where he shows off some speculative syntax for consteval mutable variables and how they could help with what he had shown off in the rest of the blogpost. And he points out that Zig already has such variables in the form of its comptime var feature.
13
u/friedkeenan May 08 '26
This library is centered around the same basic concept that I showed in a previous post, but with I think a fair number of improvements from the proof-of-concept code which was shown there.
I had been sitting on this library for a bit now while I tried to hammer out a blogpost about it, where I'd discuss a couple interesting points of the implementation, but it just wasn't coming out like how I wanted and I was getting increasingly annoyed with writing it, so I decided just to post the repo here instead.
9
u/SuperV1234 https://romeo.training | C++ Mentoring & Consulting May 08 '26
Would it be possible to use this to replace the use of
__COUNTER__in my macro-based coroutines, or is there a technical reason that wouldn't work?Don't have access to a C++26 compiler at the moment to test it out.
4
u/katzdm-cpp May 08 '26
> due to a particular technique used in the implementation of this library
What's the technique? Haha
2
u/BucketOfWood May 10 '26
Stateful metaprogramming via friend injection? If so, nice! I'd like to see it normalized as a strange but powerful technique like CRTP and SFINAE. Currently, I don't feel safe enough to use it in production due to the fact that the standards committee hates stateful metaprogramming.
63
u/parkotron May 08 '26
Alternate name suggestion:
constevilJokes aside, this is pretty neat, but I dearly hope to never be in a situation where I need something like it.
1
u/_bstaletic May 08 '26
Just a few days ago I had a discussion with a friend about a certain problem when it comes to producing python bindings with reflections. The solution required one of two things:
You even made a container for this. I told my friend I won't get into stateful metaprogramming but
cvllooks tempting.To elaborate, what we were discussing was generating the "trampolines" for pybind11 bindings.
https://pybind11.readthedocs.io/en/stable/advanced/classes.html
The short story is: python bindings need to support abstract classes, yet those are non-constructible on their own, so how could python hold a reference to the base object? The solution are the "trampoline" classes. See the pybind docs for examples. To be perfectly clear, these trampolines are 100% boilerplate and the only boilerplate that I couldn't get rid of with C++26.
C++26 can't really generate those, because
define_aggregatecan only produce simple aggregates, so my friend and I came up with this:And then construct a consteval, mutable registry of what have we seen, so that when
bind_class<T>(module)is called, you look through the registry and apply the trampoline.3
u/[deleted] May 09 '26
[removed] — view removed comment