3 ms·
Deep copy is pedagogically and semantically the right choice for any mutable containers. You either make containers immutable or copies deep. Otherwise it's jus
by kccqzy 1y ago
Deep copy is pedagogically and semantically the right choice for any mutable containers. You either make containers immutable or copies deep. Otherwise it's just an invitation for subtle bugs.
- criemen 1y agoI'm not sure about that - every time I copy an object I have to think through what happens, no matter the default semantics. C++ makes the deep copy case easier than other programming languages without top-level built-in support.
- hgomersall 1y agoNo, it should move properly when passing by value (as in, essentially the rust move semantics). If you want a copy, that should be explicit.
- hyghjiyhu 1y agoMoving by default would be too much of a footgun without a borrow checker imo.
- hgomersall 1y agoI think using a language without a borrow checker is already a massive footgun (albeit less of one for GC/RC languages). More sensible move semantics would still be a big ergonomic improvement.
- kccqzy 1y agoI think the language allowing use-after-move is honestly a smaller footgun than allowing using uninitialized memory or use-after-free. C++ already has the latter.
- cjfd 1y agoIt certainly makes things easier. But it also makes some things very, very, very inefficient. I want a list with millions/billions of elements. I want to regularly change one of the elements somewhere in the middle. Good luck with the copying.
- vlovich123 1y agoThen explain that const isn’t deep and a const container can end up mutating state? Pretending like c++ has a consistent philosophy is amusing and pretending this happened because of pedagogy is amusing. It happened because in c assignment is a copy and c++ inherited this regardless of how dumb it is as a default for containers.
- kccqzy 1y agoIn C++ regular types have the property that const is deep. If you have a const std::vector<int> the you can't modify any of the integers contained in this container. Naturally for flexibility reasons not all types are regular, pointers being the prominent exception, and things like std::string_view being modern examples of non-regular types.
- vlovich123 1y agoI feel like you could benefit from watching Scott Meyers about the silliness in C++ if you feel like there’s a consistent and logical feel to the language. A lot of this is c++-isms masquerading sensible ideas through official terms (regular and non-regular types)
- kccqzy 1y agoOh I certainly do not feel like there's consistency in the language. The C++ language is such that everyone picks a subset that makes sense to them and is relatively consistent. Regular and non-regular types are however a basic idea that transcends languages. I can write a snippet in Python too: import copy from some.random.module.on.pypi import foo a = foo() b = copy.copy(a) assert a == b If that assertion fails it implies the type isn't regular.
- vlovich123 1y agoCan you point me to terminology other than the c++ spec (preferably literature) that defines regular types? I can only find references to this concept in c++ land.