Live data from Hacker News

A C++ Immutable String class

blog.reliablecpp.com

41–50 of 58 posts

Re: A C++ Immutable String class

#41
post #18

I don't really understand why you would want this other than to trick yourself into thinking you are writing java. (I already find it really annoying when people port their Javaisms to C++.)

But one of the big feature of C++ is that it lets you write in any languageisms you want! ;)

That is a big feature of any language that is multi-paradigm.

Re: A C++ Immutable String class

#42

Earlier quoted context omitted.

The page is down, but: Most languages have immutable strings. Also, I hope you're not suggesting immutability is a Javaism...

Yes, actually, I don't think it's controversial to say that Java did more to popularize immutable strings than any of the languages you're thinking of. And when people get CS degrees completely centered around Java, bring this kind of "feature" and others to C++, they end up churning out what looks a lot like pretend-Java.

Do those CS degrees exist? I pity those students.

Back then when I graduated (1999), at least in Portugal there were lots and lots of languages to use for assignments, during the degree (5 years long).

Re: A C++ Immutable String class

#43
post #25

Earlier quoted context omitted.

I'm toying with implementing O(1) copies by using a reference counted pointer to basic_string instead of storing it by value within the class. It would mean a small memory overhead and another level of indirection, but probably worth doing. Thoughts?

Most implementations are reference counted with copy on write. No point.

libc++ (the Clang project standard C++ library) doesn't use COW for std::string, and I'm pretty sure GNU stdlibc++ will also drop it the next time they have to break ABI. C++11 move semantics and the 'small string optimisation', as it's known, blow away any performance benefit of COW for most sane uses.

Re: A C++ Immutable String class

#44
post #25
post #6

I guess I expected the whole point of an immutable string class would be to have O(1) copies. What does this buy you beyond 'const std::string'?

I'm toying with implementing O(1) copies by using a reference counted pointer to basic_string instead of storing it by value within the class. It would mean a small memory overhead and another level of indirection, but probably worth doing. Thoughts?

There's no point. Just make your class movable.

Re: A C++ Immutable String class

#45

Earlier quoted context omitted.

Why (are mutable strings insane)?

OK, that comment was too light on substance. The problem with strings being mutable by default is that one has to do a lot of defensive copying to avoid unexpected behavior. Mutable strings do have their place, inside a function that's building up a string, before that string is visible to any other part of the program. But it shouldn't be the default. See also: "The Value of Values" by Rich Hickey ( http://www.infoq…

The primary reason when deciding whether a type should be mutable or not is psychological: mutable things are containers with independent identity from their content; things that are identified by their value should be immutable. The prototypical mutable type is an array; the prototypical immutable types are numbers: if you change the imaginary part of a complex number, you don't have the same number with different content, you have a different number. When you think about strings as arrays of bytes like you do in C, it makes sense for them to be mutable; in a higher level language where they behave much more like atomic values, it makes much more sense for them to be immutable – it can be really jarring when some called code deep down in the guts of a program mutates a string and you see the results at the top level. In C++, which is somewhere between a low level and high level language, it's hard to say which way it should be, but the STL approach does seem to treat them more like values than containers, which implies that they probably should be immutable.

Re: A C++ Immutable String class

#46
post #38

Earlier quoted context omitted.

That is accurate. The object reference by immutable msg is hello_world which is mutable. If the value of hello_world changes, then the value of msg changes too. In the simple example, it won't happen, but the point is that it could , especially when multi-threaded.

I don't buy it, by calling that function, the caller is saying the value won't change underneath you (because you are being called in multithreaded code). The object itself cannot change itself, though of course there can be mutable members, which tend to be syncronisation mechanisms (mutexes) anyway.

No, the caller is saying that the callee isn't permitted to change the value! That's what const means. No promise is made about the actual object itself. There's no general way to specify that in C++.

This isn't so much an issue for functions, where the caller is stopped while the callee runs, but it needs to be borne in mind for longer-lived objects that take const references - or, indeed, const pointers - to objects that have longer lifetimes again. (I don't think multithreading need be introduced for this to be an issue.)

There's quite a difference between an object that could change and one that won't, if you're considering caching values across method calls, or setting up the referring object based on the referred object's current state, etc.

Re: A C++ Immutable String class

#47
post #43

Earlier quoted context omitted.

Most implementations are reference counted with copy on write. No point.

libc++ (the Clang project standard C++ library) doesn't use COW for std::string, and I'm pretty sure GNU stdlibc++ will also drop it the next time they have to break ABI. C++11 move semantics and the 'small string optimisation', as it's known, blow away any performance benefit of COW for most sane uses.

COW is prohibited in C++11 Standard implementations of std::basic_string

Re: A C++ Immutable String class

#48
post #46

Earlier quoted context omitted.

I don't buy it, by calling that function, the caller is saying the value won't change underneath you (because you are being called in multithreaded code). The object itself cannot change itself, though of course there can be mutable members, which tend to be syncronisation mechanisms (mutexes) anyway.

No, the caller is saying that the callee isn't permitted to change the value! That's what const means. No promise is made about the actual object itself. There's no general way to specify that in C++. This isn't so much an issue for functions, where the caller is stopped while the callee runs, but it needs to be borne in mind for longer-lived objects that take const references - or, indeed, const pointers - to object…

Right, multithreading is needed for it to be an issue with functions, not with containers.

Re: A C++ Immutable String class

#50
post #44
post #25

Earlier quoted context omitted.

I'm toying with implementing O(1) copies by using a reference counted pointer to basic_string instead of storing it by value within the class. It would mean a small memory overhead and another level of indirection, but probably worth doing. Thoughts?

There's no point. Just make your class movable.

The class is movable already. Reference counting will allow a copy constructor or copy assignment to avoid copying the string object, just increase the reference count.
Post reply on HN