News for hackers. Stuff that matters.
Re:A C++ Immutable String class by haberman on Saturday September 21, 2013 @02:32PM (#6423918)
> I don't buy it, by calling that function, the caller is saying the value won't change underneath you

You really do not understand what "const" means in C++.

If what you said were true, a whole class of optimizations would be possible that are not currently possible. For example, consider this silly function:

  void f(const int *x) {
    int y = *x;
    g();
    if (y == *x) h();
  }
If what you said were true, this would be able to optimize away the call to h() completely, but if you compile the function with maximum optimization you will see that this is not the case. Why not? Because the caller could look like this:

  int val = 0;
  void g() { val = 1; }
  int main() { f(&val); }

There are no comments on this comment yet.