title: Multiple arguments to C++ placement new 'constructor'
body: <p>In order to do some custom memory tracking (leak prevention, corruption detection), I'm having to use placement new to create C++ objects, which works fine - but I'm struggling to figure out how I can pass arguments to the constructor, since it's called from a macro (so the file + line can be provided automatically).</p>
  
  <p>The function:</p>
  
  <pre><code>template &lt;typename T&gt;
  T*
  cpp_new(
      const char *file,
      size_t line
  )
  {
      T   *n = (T*)tracked_allocate(&amp;memory_context, sizeof(T), file, line);
  
      if ( n )
      {
          construct(n);
      }
      else
      {
          throw std::bad_alloc();
      }
  
      return n;
  }
  </code></pre>
  
  <p>This is called via the macro:</p>
  
  <pre><code>#define new_object(type)    cpp_new&lt;type&gt;(__FILE__, __LINE__)
  </code></pre>
  
  <p>Placemented new:</p>
  
  <pre><code>template &lt;typename T&gt;
  void
  construct(
      T *obj
  )
  {
      obj = new (obj) T;
  }
  </code></pre>
  
  <p>The va_list macros would cover the expansion for the variable number of arguments, only I don't want to supply the number of arguments the constructor has, removing va_arg(), and can't use va_start(), since it expects a format.</p>
  
  <p>This went a little over my head: <a href="http://www.drdobbs.com/cpp/calling-constructors-with-placement-new/232901023?pgno=2" rel="nofollow">http://www.drdobbs.com/cpp/calling-constructors-with-placement-new/232901023?pgno=2</a></p>
  
  <p>Is there any way I can use <code>__VA_ARGS__</code> from new_object and pass them into the construct function? Each object only has one constructor, but there are many different types of objects taking different parameters, so I want to remove as much manual maintenance as possible.</p>
  
  <p>Or is there just a better way in general of doing what I'm attempting!</p>