Re:Why introduce `std::launder` rather than have the compile...
by codeflo on Saturday January 22, 2022 @06:46AM
(#30035863)
I think you have to do
because no assignment operator is generated for a struct with a const member.
[ Parent
| on HN ]
Re:Why introduce `std::launder` rather than have the compile...
by nyanpasu64 on Sunday January 23, 2022 @05:39PM
(#30051283)
> no assignment operator is generated for a struct with a const member.
I wanted to use const fields to enforce the logical invariant that a field never changes for the lifetime of an object, until the entire object is overwritten by a newly constructed object. Unfortunately C++ deleting the move-assignment operator means it's no longer practical to do so (though you can use placement-new for POD data where it's safe to not run the destructor, and even then it's obscure and will leak memory and possibly is UB as soon as you add an owning type). Instead, I keep a mutable field and have to remember to never mutate it when writing hundreds of lines of method implementations, and when I come back to the code months later and edit it, and someday I may slip up.
Rust has no const fields at all.