Const does not protect from the problem of modifying the list by the caller afterwards. |
Pass by ref makes my code easily breakable by changes in other places of code, not directly coupled with my code. |
It breaks encapsulation and simply doesn't scale up. This is why most modern languages use immutable strings as a basic data structure for text and not mutable ones like in STL. |
Passing by const ref forces me to do costly O(n) copy whenever I need to change something in the vector/list. |
I think the problem is that you are trying to use C++ like you do other languages, particularly functional type languages. |
NUMA is a red herring. It solved the copy problem in hardware, not software. |
How exactly do you modify the list if it is const? Example please. I don't see how it "breaks encapsulation". |
|
|
our intended meaning is "have a throwaway-copy" basically, right? Doesn't an immutable string (for example) basically have to construct a new "basic string" inside of itself if you try to change it? |
It is not const from the beginning. It is const only when passed by const-ref. So it is temporarily const. After the function exits it can still be modified. |
Making throwaway copies in C++ is extremely costly - you need to copy the whole structure. If it was immutable, you could just copy a small part of it, saving lots of memory and CPU. |
|
|
Additionally making a copy constructor for a complex mutable data structure is non-trivial |
and very often a source of difficult to debug bugs. |
xoreaxeax wrote: |
---|
The problem is much more general - mutable state shared between many units is evil, regardless it is C++ or not. C++ has just poor tools to deal with this problem, so it is you, the PROGRAMMER who should care. I only warned. |
You're 'design philosophy' may be great for people studying 'computer language theory' |
xoreaxeax wrote: |
---|
Actually this design philosophy is used in practice very often also in imperative languages (in C, C++, Java, etc.) and proved itself superior to "pass pointers to mutable data everywhere". |
Once again, no one has ever suggested you should "pass pointers to mutable data everywhere" |
When you want the data to change, you use mutable data. (...) But the bottom line is that programs change data. Its what they do. |
C++ has various language tools to mitigate the dangers of changing data |
Could you give an example of a non-trivial copy ctor? I don't understand this part...it is no more difficult to make hard-to-detect bugs in a copy ctor than a normal ctor or any function really...The only time I can think of where you'd need to make a custom copy ctor is for something that is managing raw memory of some type. Other than that, the compiler generator copy ctor should be fine. |
xoreaxeax wrote: |
---|
This is entirely incorrect. It is possible to write any algorithm with no mutable data at all. |
xoreaxeax wrote: |
---|
Ok, which, except passing by val and making copies? |
Yes, and it will run like a pig on a spit. |
Once this is known, one can start looking for suitable solutions. |
In most cases the autogenerated constructor is wrong. |
|
|
Only if for some reason you want to use raw pointers. |
I think your issues with C++ stem from the fact that you don't have clear object ownership. |
If I use smart pointers, the autogenerated constructor is in most cases also wrong. |
But I've already said that in large projects written by many authors you usually don't have. |
I think you failed here...the point of a smart pointer is to manage the data for you, which means they usually have a manually created constructor, copy ctor, = operator, destructor. Ex. std::auto_ptr. |
That's what documentation/being a team is all about. If you don't have good documentation or a team that can work well together, C++ can't help you. |
|
And how this default copy constructor of universal smart pointer knows if it should copy the pointed data or not? :D The point of smart pointer is just to manage memory (in a quite inefficient way, AFAIK) and avoid leaks and dangling pointers. Not to control copying semantics. |
std::auto_ptr<some_type> my_ptr(new some_type);
Actually documentaiton sucks in 99% or more projects and developers often come and go. ;) |
How so? Do you mean it is copy-on-write? If so, then it must be slow as a pig. COW is one of the most inefficient techniques for handling small objects. |
And also, how exactly are they inefficient? |
Also, you wouldn't be "copying" data. You use smart ptrs like this |
|
|
|
|
They keep reference counts. Every pointer assignment = a test + 2 interlocked increment / decrements. On multiprocessors slow as a pig. Proper GC is much, much faster. |
|
|
I know how to use smart pointers, but imagine an object of the class... |
Superior to what? And which programmers do you find reasonable? Some great programmers stay away from C++: see http://www.google.pl/search?q=Coders+at+Work+on+C%2B%2B&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:pl:official&client=firefox-a |
I can't just run a "compiled" Java program |
GC isn't that much faster, it just (randomly) will take up time searching for memory you aren't referencing |
Erm, the point of smart pointers is that they have defined their own copy ctors etc so you don't have to do anything special when they are part of a class... |
And just because some people decide against using C++ for their projects doesn't mean it's bad |
Same as having a good optimizing compiler. |
...thus introducing semantic bugs in your program. |
And if these are probably one of the smartest programmers of these times? These are not just some random people. They know what they are saying. |
Ok, back to the topic: so what is your approach to shared mutable data? If it is only clear object ownership, then what is your approach to making 99% of documentation NOT suck? |
My approach is to have no shared mutable data, by having shared immutable data and local mutable data. This approach does not work well in C++, because STL lacks immutable data structures. So I have to fallback to "copy everything" approach, which is inefficient. |