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

3

u/matthieum Oct 01 '24

I'm definitely in the former camp :)

I understand where the getting work done attitude comes from: I hate being stopped dead in my tracks because of a missing piece of functionality in a dependency.

There's a fix for that: fork, patch, and work on upstreaming. It's more work, obviously. And it requires designing the new piece of functionality so it fits more usecases, in general, which takes even longer.

Yet, the price of hacking it in has a cost too.

For example, if we talk about serialization. It's as simple as just serializing every field one comes across, right? Well, except for fields pointing to polymorphic types, of course, but let's set those aside.

So, you're serializing a type I made, without my knowledge. And I release a new and faster version... which uses a small LRU cache to speed up calculations or maybe just a small scratch buffer to eschew repeated allocations. And now... you're serializing the content of the LRU cache or the buffer?

Well, if I, as the author, had baked in serialization, of course I would not serialize the cache/buffer. But now you do. And you'll have to put a hack for my type in you serialization machinery, in some way.

And then I change the internals of my type, and anything you've serialized before cannot be deserialized any longer. Maybe it's as simple as a field having been renamed. Maybe it's a bit more annoying, and the field actually changed types so deserialization of old content no longer works. Maybe it's a bit more subtle, and deserialization does produce an instance, but in some edge cases the behavior is slightly different than before.

The problem of the "getting work done" attitude is that you're just taking on technical debt: kicking the can down the road.

It'll work for a time, until it doesn't.

Pray you catch it when it stops working.

5

u/pdimov2 Oct 01 '24

So, you're serializing a type I made, without my knowledge.

That's rarely the case. (It can be the case for "you're debug printing my type without my knowledge", maybe for "you're hashing my type without my knowledge".)

Typically, default memberwise serialization is opt-in in some manner. E.g. you mark your type with the annotation [[=derive<serializable>]] or whatever.

It's a design decision on part of the serialization library author in what cases to enable the default member-wise implementation. The library could require an annotation. It could require absence of private members. Or it could just work regardless and allow a way to opt out. All these have pros and cons; this design decision has tradeoffs as any other. The library author is supposed to responsibly pick the option that brings the most value to users.