| ▲ | kccqzy a day 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 a day ago | parent | next [-] |
| I'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 a day ago | parent | prev | next [-] |
| No, 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 a day ago | parent [-] | | Moving by default would be too much of a footgun without a borrow checker imo. | | |
| ▲ | hgomersall 16 hours ago | parent | next [-] | | I 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 11 hours ago | parent | prev [-] | | I 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. |
|
|
|
| ▲ | vlovich123 a day ago | parent | prev | next [-] |
| Then 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 a day ago | parent [-] | | In 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 a day ago | parent [-] | | I 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 11 hours ago | parent [-] | | Oh 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. |
|
|
|
|
| ▲ | cjfd a day ago | parent | prev [-] |
| It 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. |
| |
| ▲ | Maxatar a day ago | parent [-] | | Why would you copy a whole list to modify a single element? | | |
| ▲ | cjfd 16 hours ago | parent [-] | | A very good question indeed. But maybe you should read the post I am responding to..... |
|
|