title: Why does const_cast need to state what you're casting to?
body: <p>According to the standard (§5.2.11) a const_cast casts away cv-qualifiers (const or volatile).</p>
  
  <p>Here's a simple example. First you declare two functions taking a pointer and a reference:</p>
  
  <pre><code>class Bar { ... };
  void foo_ptr(Bar*);
  void foo_ref(Bar&amp;);
  </code></pre>
  
  <p>then you create a reference-to-const:</p>
  
  <pre><code>Bar b;
  const Bar&amp; cb = b;
  </code></pre>
  
  <p>and then you can call either function by using the appropriate const_cast:</p>
  
  <pre><code>foo_ptr(const_cast&lt;Bar*&gt;(&amp;cb));
  foo_ref(const_cast&lt;Bar&amp;&gt;(cb));
  </code></pre>
  
  <p>Here's my question: since const_cast cannot do what the other casts were designed for, isn't it obvious what you're casting to? In other words, why doesn't the language allow me to simply say:</p>
  
  <pre><code>foo_ptr(const_cast&lt;&gt;(&amp;cb));
  foo_ref(const_cast&lt;&gt;(cb));
  </code></pre>
  
  <p>I can think of only the following two reasons:</p>
  
  <p>a) The compiler should stop me when I try to do something crazy, like:</p>
  
  <pre><code>foo_ptr(const_cast&lt;int*&gt;(&amp;cb));
  foo_ref(const_cast&lt;int&amp;&gt;(cb));
  </code></pre>
  
  <p>and by forcing me to explicitly state the type I'm casting to it can then keep me from misbehaving. I find this (hypothetical) explanation weak, since it would be bizarre if the language favored allowing me to write down something wrong only to have the compiler correct me.</p>
  
  <p>b) There is a possible ambiguity if the variable is both const and volatile. In that case the compiler would have no way of telling if I'm trying to cast away the one or the other (or both).</p>
  
  <p>Is that why, or is there another reason?</p>