title: Cast away volatile from fundamental type in C++
body: <p>What is the proper syntax (if there even is any) for casting away <code>volatile</code> from a fundamental type?</p>
  <p>Suppose I have the following code:</p>
  <pre><code>void Bar(const int&amp; x) {}
  volatile int foo;
  
  int main() {
      Bar(const_cast&lt;int&gt;(foo));   // (1)
      Bar(const_cast&lt;int&amp;&gt;(foo));  // (2)
      Bar(const_cast&lt;int&amp;&amp;&gt;(foo)); // (3)
      Bar((int)foo);               // (4)
  }
  </code></pre>
  <p>The C++-style cast in (1) produces an error in VS 2015, but not in gcc 7 with <code>-std=c++11</code>. Judging by the C++ standard, section [expr.const.cast] (5.2.11 in C++11), I tend to agree with VS there, as I don't see what paragraph in that section would allow performing this explicit cast (and paragraph 1 disallows any other explicit conversions).</p>
  <p>Now (2) and (3) should be perfectly fine as per the standard; however, if I understand the C++ standard correctly, the result of such a conversion would be a non-volatile reference to the volatile variable, and thus accessing it would result in undefined behavior.</p>
  <p>As for the C-style cast in (4), I'm surprised that it doesn't also produce an error in VS 2015, as section [expr.cast] (5.4 in C++11) of the standard appears to explicitly designate such a cast as ill-formed, as it cannot be synthesized using C++-style casts.</p>