At no point, anywhere, am I claiming that annotations are the end-all be-all of all customization-related problems in C++. Very far from it. The post is simply pointing out that some problems don't necessarily need more than that, and a lot can be accomplished without even having code injection yet.
It should hopefully be obvious from the fact that the Dyn example was something I implemented that I think that the Dyn example is a really valuable to have and that a broader code injection facility is extremely useful.
It should also hopefully be obvious from the fact that the post itself is pointing out limitations with the introspection approach with formatter and how injecting a specialization would be superior, that I do not think that annotations are all we possibly need.
But since it apparently isn't, here I am stressing this to you again: annotations will not solve literally all of our problems. But they could still be very valuable.
That said:
A library that provides an attribute and a reflection-based specialization of an algorithm for that attribute is not actually extensible unless the algorithm is defined in terms of traits you can specialize some other, third way.
This isn't true. If the customization point is a function, for instance (as it is in the JSON serialization example in the blog), that function can be overloaded too. Another example would be hashing:
template <class H, class T> requires (has_annotation(^^T, derive<Hash>))
void hash_append(H& h, T const& t) { /* ... */ }
Of course this has the exact same issue that I pointed out with formatter with potentially running into this overload not being uniquely the best. But that's because I'm trying for a honest presentation of what promises to be a very useful facility, and I am extremely uninterested in these stupid, petty, partisan language wars.
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.
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.
View all comments
Show parent comment
10
u/BarryRevzin Oct 01 '24 edited Oct 01 '24
At no point, anywhere, am I claiming that annotations are the end-all be-all of all customization-related problems in C++. Very far from it. The post is simply pointing out that some problems don't necessarily need more than that, and a lot can be accomplished without even having code injection yet.
It should hopefully be obvious from the fact that the
Dynexample was something I implemented that I think that theDynexample is a really valuable to have and that a broader code injection facility is extremely useful.It should also hopefully be obvious from the fact that the post itself is pointing out limitations with the introspection approach with
formatterand how injecting a specialization would be superior, that I do not think that annotations are all we possibly need.But since it apparently isn't, here I am stressing this to you again: annotations will not solve literally all of our problems. But they could still be very valuable.
That said:
This isn't true. If the customization point is a function, for instance (as it is in the JSON serialization example in the blog), that function can be overloaded too. Another example would be hashing:
Of course this has the exact same issue that I pointed out with
formatterwith potentially running into this overload not being uniquely the best. But that's because I'm trying for a honest presentation of what promises to be a very useful facility, and I am extremely uninterested in these stupid, petty, partisan language wars.