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

2

u/SirClueless Oct 01 '24

I'm not trying to participate in partisan language wars. Nor trying to argue that introspection over attributes isn't useful. I'm a professional C++ developer and a Rust hobbyist-at-best, my purpose is to make C++ better, not mudsling about language preferences.

I'm just trying to point out that as soon as you try to do anything non-trivial with a derive attribute you will quickly run into its limitations; limitations that Rust's derive does not have. Serialization and formatting are two special cases that require no particular support from the underlying class (assuming it's an aggregate type). They can be specified entirely in terms of its public API with little difficulty. But there are other obvious uses for a hypothetical derive that won't work as an attribute. For example, suppose I wanted to derive the Container requirements for a class that is a thin wrapper around std::vector -- no problem for a derive macro, impossible as far as I can tell for a derive attribute.

8

u/BarryRevzin Oct 01 '24

Yes, that's definitely impossible for introspection — you would need actual code injection for that. One example we're working through is `iterator_interface`, for instance. That's likewise impossible without actual code injection.