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...
A C++ Immutable String class
21–30 of 58 posts
Re: A C++ Immutable String class
#22Earlier 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.
Re: A C++ Immutable String class
#23Re: A C++ Immutable String class
#24This 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.
Re: A C++ Immutable String class
#25I 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'?
Re: A C++ Immutable String class
#26Earlier 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?
Re: A C++ Immutable String class
#27Re: A C++ Immutable String class
#28This 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)?
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
#29Earlier 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…
Re: A C++ Immutable String class
#30I 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?