3 ms·
>and tie object's lifetime to where it keeps its state What's the objection there? I want the state to be freed when the object's lifetime ends. For something
by Thorrez 4y ago
>and tie object's lifetime to where it keeps its state
What's the objection there? I want the state to be freed when the object's lifetime ends. For something only needed during a function, a local value variable makes sense. For something that needs to last longer, either give it a move operator, or put it on the heap and use unique_ptr.
If your point is that people sometimes use unique_ptr<std::string> instead of std::string or std::optional<std::string>, then yeah I agree, that's generally not good.
- eska 4y agoThat’s not his point, but you demonstrated it quite well. C++ use leads to developers that conflate memory allocation and initialization. For contrast, objective C also differentiates between the two.
- hedora 4y agoAllocation and initialization should be atomic. Otherwise, the rest of the program has to deal with uninitialized objects and no invariants hold.