title: Can C's restrict keyword be emulated using strict aliasing in C++?
body: <h2>The Problem</h2>
  
  <p>The <code>restrict</code> keyword in C is missing in C++, so out of interest I was looking for a way to emulate the same feature in C++. </p>
  
  <p>Specifically, I would like the following to be equivalent:</p>
  
  <pre><code>// C
  void func(S *restrict a, S *restrict b)
  
  // C++
  void func(noalias&lt;S, 1&gt; a, noalias&lt;S, 2&gt; b)
  </code></pre>
  
  <p>where <code>noalias&lt;T, n&gt;</code></p>
  
  <ul>
  <li>behaves just like <code>T*</code> when accessed with <code>-&gt;</code> and <code>*</code></li>
  <li>can be constructed from an <code>T*</code> (so that the function can be called as <code>func(t1, t2)</code>, where <code>t1</code> and <code>t2</code> are both of type <code>T*</code>)</li>
  <li>the index <code>n</code> specifies the "aliasing class" of the variable, so that variables of type <code>noalias&lt;T, n&gt;</code> and <code>noalias&lt;T, m&gt;</code> may be assumed never to alias for n != m.</li>
  </ul>
  
  <h2>An Attempt</h2>
  
  <p>Here is my deeply flawed solution:</p>
  
  <pre><code>template &lt;typename T, int n&gt;
  class noalias
  {
      struct T2 : T {};
      T *t;
  
  public:
      noalias(T *t_) : t(t_) {}
      T2 *operator-&gt;() const {return static_cast&lt;T2*&gt;(t);} // &lt;-- UB
  };
  </code></pre>
  
  <p>When accessed with <code>-&gt;</code>, it casts the internally-stored <code>T*</code> to a <code>noalias&lt;T, n&gt;::T2*</code> and returns that instead. Since this is a different type for each <code>n</code>, the strict aliasing rule ensures that they will never alias. Also, since <code>T2</code> derives from <code>T</code>, the returned pointer behaves just like a <code>T*</code>. Great!</p>
  
  <p>Even better, the code compiles and the assembly output confirms that it has the desired effect.</p>
  
  <p>The problem is the <code>static_cast</code>. If <code>t</code> were really pointing to an object of type <code>T2</code> then this would be fine. But <code>t</code> points to a <code>T</code> so this is UB. In practice, since <code>T2</code> is a subclass which adds nothing extra to <code>T</code> it will probably have the same data layout, and so member accesses on the <code>T2*</code> will look for members at the same offsets as they occur in <code>T</code> and everything will be fine.</p>
  
  <p>But having an <code>n</code>-dependent class is necessary for strict aliasing, and that this class derives from <code>T</code> is also necessary so that the pointer can be treated like a <code>T*</code>. So UB seems unavoidable.</p>
  
  <h2>Questions</h2>
  
  <ul>
  <li><p>Can this be done in c++14 without invoking UB - possibly using a completely different idea?</p></li>
  <li><p>If not, then I have heard about a "dot operator" in c++1z; would it be possible with this?</p></li>
  <li><p>If the above, will something similar to <code>noalias</code> be appearing in the standard library?</p></li>
  </ul>