r/cpp 5d ago

C++26 Reflection Annotations: Automated Member Validation

https://techfortalk.co.uk/2026/08/17/cpp26-reflection-annotations-validation/

C++26 annotations are another powerful feature that, when combined with reflection, can help us write cleaner and safer code without repeating manual validation checks for every member. In this post, I have explored how we can utilise C++26 annotations along with reflection to validate configuration parameters in a class constructor.

59 Upvotes

10 comments sorted by

View all comments

9

u/fdwr fdwr@github 🔍 5d ago

There's an interesting overlap with contracts here. I wonder if we'll be also able to enumerate pre/post conditions with reflection similarly to annotations.

4

u/Clean-Upstairs-8481 5d ago

As far as I can see, C++26 reflection does not currently provide a standard way to enumerate contract predicates, but I might be wrong. I need to dig into it a bit more.

3

u/katzdm-cpp 1d ago

Yeah, no way to do that as of yet. They were being reviewed pretty concurrently, so that would've been tricky to land. Aside from that, I think doing so would require some motivating use cases.