title: How to verify the validity of reinterpret_cast at compile time
body: <p>I need a way to verify <strong>during compile time</strong> that upcast/downcast of a pointer to another class (either derived or base) does not change the pointer value. That is, the cast is equivalent to <code>reinterpret_cast</code>.</p>
  
  <p>To be concrete, the scenario is the following: I have a <code>Base</code> class and a <code>Derived</code> class (obviously derived from <code>Base</code>). There is also a template <code>Wrapper</code> class that consists of a pointer to the class specified as a template parameter.</p>
  
  <pre><code>class Base
  {
      // ...
  };
  
  class Derived
      :public Base
  {
      // ...
  };
  
  template &lt;class T&gt;
  class Wrapper
  {
      T* m_pObj;
      // ...
  };
  </code></pre>
  
  <p>In some situations I have a variable of type <code>Wrapper&lt;Derived&gt;</code>, and I'd like to call a function that receives a (const) reference ro <code>Wrapper&lt;Base&gt;</code>. Obviously there is no automatic cast here, <code>Wrapper&lt;Derived&gt;</code> is not derived from <code>Wrapper&lt;Base&gt;</code>.</p>
  
  <pre><code>void SomeFunc(const Wrapper&lt;Base&gt;&amp;);
  
  Wrapper&lt;Derived&gt; myWrapper;
  // ...
  
  SomeFunc(myWrapper); // compilation error here
  </code></pre>
  
  <p>There are ways to handle this situation within the scope of the standard C++. Like this for example:</p>
  
  <pre><code>Derived* pDerived = myWrapper.Detach();
  
  Wrapper&lt;Base&gt; myBaseWrapper;
  myBaseWrapper.Attach(pDerived);
  
  SomeFunc(myBaseWrapper);
  
  myBaseWrapper.Detach();
  myWrapper.Attach(pDerived);
  </code></pre>
  
  <p>But I don't like this. Not only this demands an awkward syntax, but it also produces an extra code, because <code>Wrapper</code> has a non-trivial d'tor (as you may guessed), and I'm using exception handling. OTOH if the pointer to <code>Base</code> and <code>Derived</code> are the same (like in this example, since there's no multiple inheritance) - one may just cast <code>myWrapper</code> to the needed type and call <code>SomeFunc</code>, and it will work!</p>
  
  <p>Hence I've added the following to <code>Wrapper</code>:</p>
  
  <pre><code>template &lt;class T&gt;
  class Wrapper
  {
      T* m_pObj;
      // ...
  
      typedef T WrappedType;
  
  
      template &lt;class TT&gt;
      TT&amp; DownCast()
      {
          const TT::WrappedType* p = m_pObj; // Ensures GuardType indeed inherits from TT::WrappedType
  
          // The following will crash/fail if the cast between the types is not equivalent to reinterpret_cast
          ASSERT(PBYTE((WrappedType*)(1)) == PBYTE((TT::WrappedType*)(WrappedType*)(1)));
  
          return (TT&amp;) *this; // brute-force case
      }
  
      template &lt;class TT&gt; operator const Wrapper&lt;TT&gt;&amp; () const
      {
          return DownCast&lt;Wrapper&lt;TT&gt; &gt;();
      }
  };
  
  
  Wrapper&lt;Derived&gt; myWrapper;
  // ...
  
  // Now the following compiles and works:
  SomeFunc(myWrapper);
  </code></pre>
  
  <p>The problem is that in some cases the brute-force cast is not valid. For instance in this case:</p>
  
  <pre><code>class Base
  {
      // ...
  };
  
  class Derived
      :public AnotherBase
      ,public Base
  {
      // ...
  };
  </code></pre>
  
  <p>Here the value of the pointer to <code>Base</code> differs from <code>Derived</code>. Hence <code>Wrapper&lt;Derived&gt;</code> is not equivalent to <code>Wrapper&lt;Base&gt;</code>.</p>
  
  <p>I'd like to detect and prevent attempts of such an invalid downcast. I've added the verification (as you may see), but it works in <strong>run-time</strong>. That is, the code would compile and run, and during the runtime there'll be a crash (or a failed assertion) in debug build.</p>
  
  <p>This is fine, but I'd like to catch this during compile time and fail the build. A sort of a STATIC_ASSERT.</p>
  
  <p>Is there a way to achieve this?</p>