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";
A C++ Immutable String class
11–20 of 58 posts
Re: A C++ Immutable String class
#12It's immutable but it defines swap? That seems.. odd.
Re: A C++ Immutable String class
#13Re: A C++ Immutable String class
#14Re: A C++ Immutable String class
#15This 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
Re: A C++ Immutable String class
#16Re: A C++ Immutable String class
#17I 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++.)
Also, I hope you're not suggesting immutability is a Javaism...
Re: A C++ Immutable String class
#18I 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++.)
Re: A C++ Immutable String class
#19The 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
#20I 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 don't mention Java ;)