title: Get size of polymorphic object
body: <p>I want to be able to get the size of polymorphic object. At the moment I got this:</p>
  
  <pre><code>struct Base {
      virtual std::size_t size() const {
          return sizeof(*this);
      }
  };
  
  struct Derived : Base {
      virtual std::size_t size() const {
          return sizeof(*this);
      }
  };
  </code></pre>
  
  <p>Which is literally copy &amp; paste. I want to do better. Suppose I really hate macros and CRTP seems like the only sensible approach. Let us give it a try:</p>
  
  <pre><code>struct SizedBase {
      virtual std::size_t size() const = 0;
  };
  
  template &lt;typename Type&gt;
  struct Sized : virtual SizedBase {
      std::size_t size() const override {
          return sizeof(Type);
      }
  };
  
  struct Base : Sized&lt;Base&gt; {}; 
  struct Derived : Base, Sized&lt;Derived&gt; {};
  </code></pre>
  
  <p>This looks much better, but sadly is ill-formed: <code>Derived</code> contains two <em>final overriders</em> for <code>size()</code> from <code>Base</code> and from <code>Sized&lt;Derived&gt;</code>. We can solve this by inheriting <em>through</em> <code>Sized</code>:</p>
  
  <pre><code>struct SizedBase {
      virtual std::size_t size() const = 0;
  };
  
  template &lt;typename Type, typename... SizedBases&gt;
  struct Sized : virtual SizedBase, SizedBases... {
      std::size_t size() const override {
          return sizeof(Type);
      }
  };
  
  struct Base : Sized&lt;Base&gt; {}; 
  struct Derived : Sized&lt;Derived, Base&gt; {}; 
  </code></pre>
  
  <p>This works as intended, however gets somewhat confusing in the event of multiple inheritance and prohibits altering accessibility/virtualness of bases.</p>
  
  <p>So, is there a better way?</p>