Live data from Hacker News

A C++ Immutable String class

blog.reliablecpp.com

11–20 of 58 posts

Re: A C++ Immutable String class

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

immutable_string s("hello world."); reinterpret_cast (s) += "I'm evil";

:) Ok. Right. Nothing's holy in C++.

Re: A C++ Immutable String class

#15
post #7
post #3

This defeats the purpose of std::string const s("Hello");

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

#17

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

Re: A C++ Immutable String class

#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! ;)

Re: A C++ Immutable String class

#19
The main question I have is, why?

The author also has a rather [confusing article][1] about immutable vs const, that I would discredit personally. He claims that objects accessed via reference to const can modify themselves, which is not true, as you can only access const functions through a const reference.

[1]: http://blog.reliablecpp.com/2013/09/immutable-vs-const/

Re: A C++ Immutable String class

#20

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

I had a problem with the traffic volumes! It's back now, and stable.

I don't mention Java ;)

Post reply on HN