Live data from Hacker News

A C++ Immutable String class

blog.reliablecpp.com

1–10 of 58 posts

Re: A C++ Immutable String class

#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 

Re: A C++ Immutable String class

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

The point is that one shouldn't do it accidentally. When you're explicitly using const_cast, it is fine.

Re: A C++ Immutable String class

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

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