title: Is a C++ object layout necessarily statically defined?
body: <p>More specifically, assuming <code>A</code> is an accessible base class of <code>B</code>, does the following code produce undefined behavior, and is the assertion guarenteed not to fire according to the standard?</p>
  
  <pre><code>void test(B b1, B b2) {
    A* a2 = &amp;b2;
    auto offset = reinterpret_cast&lt;char*&gt;(a2) - reinterpret_cast&lt;char*&gt;(&amp;b2);
    A* a1 = reinterpret_cast&lt;A*&gt;(reinterpret_cast&lt;char*&gt;(&amp;b1) + offset);
    assert(a1 == static_cast&lt;A*&gt;(&amp;b1));
  }
  </code></pre>
  
  <hr>
  
  <p><strong>Edit</strong>:
  I'm aware that all of the common compiler vendors implement C++ object layout (even when taking into account virtual inheritence) in a way that is compatible with the implicit assumptions of <code>test</code>. What I'm looking for is a guarantee (either implicit or explicit) for this behavior in the standard. Alternatively, a reasonably detailed description of the extent of object storage layout guarantees provided by the standard, as proof that this behavior is not guaranteed, will also be accepted.</p>