title: Is it safe to remove const via const_cast and invoke a non-const function that does not modify the resulting object?
body: <p>I know that casting away <code>const</code>-ness should be done with care, and any attempt to remove <code>const</code>-ness from an initially <code>const</code> object followed by modifying the object results in undefined behaviour. What if we want to remove <code>const</code>-ness so that we may invoke a non-const function which doesn't modify the object? I know that we should actually mark such function <code>const</code>, but suppose I'm using a "bad" code that doesn't have the <code>const</code> version available. </p>
  
  <p>So, to summarize, is the code below "safe"? My guess is that as long as you don't end up modifying the object you're ok, but I'm not 100% sure.</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  struct Foo
  {
      void f() // doesn't modify the instance, although is not marked const
      {
          std::cout &lt;&lt; "Foo::f()" &lt;&lt; std::endl;
      }
  };
  
  int main()
  {
      const Foo foo;
      const_cast&lt;Foo&amp;&gt;(foo).f(); // is this safe?
  }
  </code></pre>