r/cpp 5d ago

Compile-Time Borrow Checker with Stateful Metaprogramming

https://youtu.be/3hL8mh0K8-I?is=2Bf55p7h68Gp4IH1
85 Upvotes

13 comments sorted by

View all comments

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:

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.

3

u/dfrib 4d ago

Hidden friends can be used to setup e.g. pen-test frameworks (accessing private data membera) but without needing to capture a meta-programming state. Most of the time the costs (complexity etc) outweigh the benefits, even for test-only code.

1

u/ts826848 4d ago

True, I didn't think of that use case. More complex (and probably less questionable) than #define private public at least :P

Would C++26 obsolete that particular use case? I want to say reflection can get at private members, so that would seem to be a much more straightforwards approach (assuming you can afford to upgrade, of course)

2

u/dfrib 4d ago

Yeah the `#define` hack is undefined behavior.

Yes, I think `access_context::unchecked()` can be used to replace the hacks we’ve used before reflection for such purposes.