3 ms·
I think you have to do void some_func(foo *f) { new(f) foo { 5, 5 }; } because no assignment operator is generated for a struct with a const member.
by codeflo 5y ago
I think you have to do
void some_func(foo *f) {
new(f) foo { 5, 5 };
}
because no assignment operator is generated for a struct with a const member.
- nyanpasu64 5y ago> 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.