Live data from Hacker News

A C++ Immutable String class

blog.reliablecpp.com

21–30 of 58 posts

Re: A C++ Immutable String class

#21

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++.)

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.

Re: A C++ Immutable String class

#22
post #7

Earlier quoted context omitted.

You specify that the place s is const, not the value it keeps. You can still do: #include #include // not your code void innocentF(std::string const& s) { (const_cast (s)) += std::string(" I'm evil."); } // your code int main() { std::string const s("hello world."); innocentF(s); std::cout

That code isn't valid C++. Modifying a const object is undefined behavior. You would have to remove the const on your string in main to make the program well-defined.

const_cast is undefined behavior? Can you cite the spec?

Re: A C++ Immutable String class

#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?

Re: A C++ Immutable String class

#26
post #22

Earlier quoted context omitted.

That code isn't valid C++. Modifying a const object is undefined behavior. You would have to remove the const on your string in main to make the program well-defined.

const_cast is undefined behavior? Can you cite the spec?

const_cast is undefined behavior if the actual object is const. If it's a mutable object that just happened to be passed as a reference you can const_cast it. Or that's how I understand it.

Re: A C++ Immutable String class

#28

This implies that C++'s std:;string is mutable. I think I'll continue to run the other way, to avoid C++ (and C as well) whenever I can. Mutable strings are insane.

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.com/presentations/Value-Values)

Re: A C++ Immutable String class

#29

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…

I disagree. Variables are mutable in C++, unless otherwise stated. Interfaces can indicate they will not modify a value by taking a const (reference).

Re: A C++ Immutable String class

#30
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?

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