title: Cast to child object for testing - is this code standard-conforming?
body: <h3>TL;DR</h3>
  <p>Is this casting/function call legal, standard-conforming C++11/14 code?<br>
  If not, would it be if there were no virtual functions (if <code>std::is_standard_layout&lt;Module&gt;</code> became true)?<br>
  (<em>NB: It works on every compiler I've tested it so far...</em>)</p>
  <pre class="lang-cpp prettyprint-override"><code>class Module
  {
  protected:
      virtual float protectedVirtualFunction(float f) { return f*f*f; }
      float protectedFunction(float f) { return f*f; }
  };
  
  class ModuleTester : public Module // consists of ONLY aliases
  {
  public:
      using Module::protectedFunction;
      using Module::protectedVirtualFunction;
  };
  
  int main()
  {
      Module module;  //assume this is a pre-existing instance
      // Is this legal?
      ModuleTester* testerMask = static_cast&lt;ModuleTester*&gt;(&amp;module); 
      testerMask-&gt;protectedFunction(4.4f);
      testerMask-&gt;protectedVirtualFunction(4.4f);
  }
  </code></pre>
  <h3>Additional Information</h3>
  <p>Usually, I aim to test a classes public API only when writing UnitTests.
  In some cases - like when you're dealing with legacy code you can't change - it's just more practical to access private members.</p>
  <p>So, assuming we cannot change the design (DI, decoupling...), I see the following solutions:</p>
  <ol>
  <li>Make private members public (compromising all encapsulation)</li>
  <li>The ol' trick: <code>#define private public</code> in test context</li>
  <li>The &quot;Tester&quot; pattern (as used in example code)</li>
  </ol>
  <p>The standard way of using this &quot;Tester&quot; pattern would be like, and that should be legal:</p>
  <pre class="lang-cpp prettyprint-override"><code>    ModuleTester tester;
      tester.protectedFunction(4.4f);
      tester.protectedVirtualFunction(4.4f);
  </code></pre>
  <p>However, sometimes I have an existing instance and it would be great if I could just apply this &quot;tester mask&quot; on top of it to gain access.</p>
  <p>My guess is that as soon as I virtual functions and stop being &quot;standard layout&quot;, there could be &quot;undefined behaviour&quot; strictly speaking. <em>However</em>, if I'm only defining aliases in the derived class, I don't see how this could go wrong as long as I use the same compiler for Module and ModuleTester.</p>
  <p><strong>EDIT:</strong> I found a similar method that doesn't use explicit pointer casting (<a href="https://stackoverflow.com/a/1725107/649700">https://stackoverflow.com/a/1725107/649700</a>). It works, is arguably less readable and probably does not change the 'legality' status.</p>
  <pre><code>(module.*&amp;ModuleTester::protectedVirtualFunction)(4.4f);
  </code></pre>
  <p>Other SO questions that make some good points:
  <a href="https://stackoverflow.com/questions/30617519/reinterpret-cast-from-object-to-first-member">reinterpret_cast from object to first member</a>,
  <a href="https://stackoverflow.com/questions/1127616/how-do-i-unit-test-a-protected-method-in-c">How do I unit test a protected method in C++?</a></p>