title: specializing operator new and operator delete of some class
body: <p>In order to selectively trace the allocations of particular types, I have created the following construct:</p>
  <pre class="lang-cpp prettyprint-override"><code>struct my_alloc {
      static void* operator new(size_t sz) { /*...*/ ; return calloc(1,sz); }
      static void operator delete(void *ptr) { /*...*/ ; return free(ptr); }
  };
  </code></pre>
  <p>and whenever I wanted to follow the internal allocations of a particular type, say <code>struct A</code>, I could simply inherit from <code>my_alloc</code>:</p>
  <pre class="lang-cpp prettyprint-override"><code>struct A : my_alloc  
  {  
      AImpl* p_impl;
      A() {
          p_impl = new AImpl; // will call my_alloc::operator new
      }
      ~A() {
          delete p_impl;
      }
  };
  
  A x();  // x.p_impl has been allocated via my_alloc
  </code></pre>
  <p>This was also useful to trace allocations in std containers:</p>
  <pre class="lang-cpp prettyprint-override"><code>using Vector = std::vector&lt;some_class, my_alloc&gt;;
  
  Vector v; 
  v.reserve(10);   // v.data has been allocated using my_alloc
  </code></pre>
  <p>However, sometimes I need to trace the allocations and I sometimes I don't.
  To allow that, I tried passing the allocator as a template parameter</p>
  <pre class="lang-cpp prettyprint-override"><code>template &lt;class ALLOC&gt;
  struct A : ALLOC  {   /*...*/   }; 
  </code></pre>
  <p>but it doesn't always do the trick:</p>
  <pre><code>using A_alloced = A&lt;my_alloc&gt;;       // works fine
  using A_non_alloced = A;             // does not does not compile
  </code></pre>
  <p>I also considered providing a default parameter:</p>
  <pre class="lang-cpp prettyprint-override"><code>template &lt;class ALLOC = std::allocator&gt;
  struct A : ALLOC  {   /*...*/   }; 
  </code></pre>
  <p>but I am missing std::allocator's parameter.</p>
  <ul>
  <li><strong>Should I change the way I parameterize <code>struct A</code> (perhaps not inherit from <code>my_alloc</code>) ? , or</strong></li>
  <li><strong>should I change the definition of<code>my_alloc</code> in order to achieve both the current functionality and the ability to customize <code>struct A</code> ?</strong></li>
  </ul>
  <p><strong>PS:</strong> I am looking for a formulation that would also work for the following type-erased class! Note the <code>new Model&lt;B&gt;()</code> call, where <code>Model&lt;T&gt;</code> is not visible outside the class...</p>
  <pre class="lang-cpp prettyprint-override"><code>class MyType {
      struct Concept {
           virtual ~Concept() = default;
           virtual Concept* clone() const = 0;
      };
  
      template &lt;class T&gt;
      struct Model {
           T data;
  
           virtual Concept* clone() const { return new Model(*this); }
  
           template &lt;class B&gt; 
           Model(B value) : data(value) {}
      };
  
      Concept *self;
  public:
      template &lt;class B&gt;
      MyType(B value) : self(new Model&lt;B&gt;(value)) {}
     
      MyType(const MyType &amp;rhs) : self(rhs.self-&gt;clone()) { }
      
  };
  </code></pre>