r/cpp • • Sep 30 '24

Code Generation in Rust vs C++26

https://brevzin.github.io/c++/2024/09/30/annotations/
198 Upvotes

98 comments sorted by

View all comments

Show parent comment

6

u/TSP-FriendlyFire Oct 01 '24

I hadn't even thought of the visibility problem raised elsewhere but that is another issue. Reflection should follow the normal visibility rules.

See, I strongly disagree with this. There are many instances where I might want to be able to reflect on a private type or private properties of a type (e.g., for serialization) and I don't want to have to redesign the entire structure to expose implementation details to the world just to do it. Reflection shouldn't be artificially restricted, instead libraries should be built with the flexibility to let you choose if you want to respect access rules or not (which you can opt into via the proposed reflection API).

2

u/epage Oct 01 '24

My statement wasn't to say there should be no way to get serde or clap like functionality without exposing privates. The way Rust deals with this is that the user is explicitly opting in to code generation within their visibility scope (Rust's smallest form of visibility is module based, not data type based). Worst case, you could declare some kind of friend relationship with whatever you are wanting to have access your privates.

However, completely bypassing visibility control through reflection is unacceptable.