title: Convert reference to another type's reference (first_struct& to second_struct&) C++
body: <p>Consider my implementation returns types of</p>
  
  <pre><code>struct first_struct {  TKey key; TValue value;}
  </code></pre>
  
  <p>The interface (it's a homework) requests types of</p>
  
  <pre><code>struct pair { TKey first; TValue second;}
  </code></pre>
  
  <p>Me implementing the interface iterator:</p>
  
  <pre><code>...
  pair&amp; operator*() const {
      return *reinterpret_cast&lt;pair*&gt;(&amp;*iter);  // smooth?
  }
  
  private:
  Implementation::iter;  // operator* returns `first_struct&amp;`, it's my implementation
  </code></pre>
  
  <p>Is this really the cleanest way to do this?
  I also thought about making a conversion operator from <code>first_struct</code> to <code>pair&amp;</code> but 1) first_struct should not care 2) in the operator, I have no idea how to point the data members of pair to the first_struct members' location, also pair by the assignment has data members as value types, not references.</p>
  
  <p>EDIT:</p>
  
  <p>This causes BAD_ACCESS, as it is, as Dan M. pointed out in comments, undefined behavior. <a href="http://en.cppreference.com/w/cpp/language/reinterpret_cast" rel="nofollow noreferrer">reinterpret_cast - cppreference</a>(I hope I understood it well) </p>
  
  <p>BUT I looked at the standard draft (because at <a href="http://en.cppreference.com/w/cpp/language/reinterpret_cast" rel="nofollow noreferrer">cppreference</a> they mentioned (in Type aliasing paragraph) char is the exception, but nothing about char* (which I use elsewhere in the code in a similar situation).
  In <a href="https://github.com/cplusplus/draft/blob/master/papers/n4741.pdf" rel="nofollow noreferrer">standard draft - github</a> </p>
  
  <blockquote>
    <p>8.5.1.10 Reinterpret cast</p>
    
    <p>§ 7 An object pointer can be explicitly converted to an object pointer of a different type. When a prvalue v of object pointer type is converted to the object pointer type “pointer to cv T”, the result is static_cast(static_cast(v)).</p>
    
    <p>§ 11 A glvalue expression of type T1, designating an object x, can be cast to the type “reference to T2” if an expression of type “pointer to T1” can be explicitly converted to the type “pointer to T2” using a reinterpret_cast. The result is that of *reinterpret_cast(p) where p is a pointer to x of type
    “pointer to T1”.</p>
  </blockquote>
  
  <p>And there's nothing about undefined behavior. If <code>static_cast&lt;cv T*&gt;(static_cast&lt;cv void*&gt;(v))</code> doesn't cause it (I hope). So it should work. Am I missing something or cppreference is wrong, or the standard is too new?</p>