r/cpp • • Aug 16 '26

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.

64 Upvotes

12 comments sorted by

View all comments

Show parent comment

2

u/serviscope_minor Sep 06 '26

I still don't see the point of auto-generated getter/setters pairs. Getters only as a way to ensure non modification of the object in question, sure. But if you have an auto-generated getter and setter, there's no real difference to a public member. The point of private members is you can maintain class invariants, but an auto-generated setter doesn't do that. IMO trivial getters and setters don't actually encapsulate anything, they just add boilerplate.

1

u/El_RoviSoft Sep 06 '26

You’re right but I mostly mean cases when you have solely getter (aka const& accessor), special cased setters (so it’s automatically guarded by mutex or you need regular accessor + special cased setter which can be effectively generated).

But yeah, when you need just const& + & accessors it’s better to make public field and that’s it.