title: C++: Passing classes to vararg function
body: <p>I'm trying to make a class that behaves like MS CString (that is, you pass it to printf and it acts just like a pointer to C string, without additional ugly black magic like ".c_str()").</p>
  
  <p>This is very first implementation of this class, that just works and doesn't yet provide anything useful:</p>
  
  <pre><code>#include &lt;cstdlib&gt;
  #include &lt;cstring&gt;
  
  class CString
  {
  protected:
      struct CStringInfo
      {
          size_t Length;
          size_t MaxLength;
      };
  
  public:
      CString()
      {
          Buffer = NULL;
  
          Assign(NULL);
      }
  
      CString(const char* chv)
      {
          Buffer = NULL;
  
          Assign(chv, 0);
      }
  
      ~CString()
      {
          if(Buffer) delete[] Buffer;
          Buffer = NULL;
      }
  
      size_t GetLength()
      {
          if(!Buffer) Alloc(1);
          return GetInfo()-&gt;Length;
      }
  
      size_t Resize(size_t size)
      {
          Alloc(size + 1); // + 0x00
          Buffer[size] = 0;
          return size;
      }
  
      bool Assign(const char* value, size_t size = 0)
      {
          size_t strl = ((size) ? size : strlen(value));
  
          if(!value || !(strl = strlen(value)))
          {
              if(!Buffer) Alloc(1);
              return false;
          }
  
          Alloc(strl + 1);
          memcpy(Buffer, value, strl);
          Buffer[strl] = 0;
          return true;
      }
  
      CString&amp; operator = (const char* what)
      {
          Assign(what);
          return (*this);
      }
  
      CString&amp; operator = (CString&amp; string)
      {
          Assign(string.Buffer);
          return (*this);
      }
  
      operator const char* ()
      {
          return Buffer;
      }
  
  protected:
      char* Buffer;
  
      void Alloc(size_t size)
      {
          if(!size) size = 1;
          char* nb = new char[size + sizeof(CStringInfo)];
          char* nbb = nb + sizeof(CStringInfo);
          size_t cl = size - 1;
          if(Buffer)
          {
              if(cl &gt; GetInfo()-&gt;Length) cl = GetInfo()-&gt;Length;
              if(cl) memcpy(nbb, Buffer, cl - 1);
              nbb[cl] = 0;
              *(CStringInfo*)(nb) = *(CStringInfo*)(Buffer);
              delete[] (Buffer - sizeof(CStringInfo));
          }
  
          Buffer = nb;
          GetInfo()-&gt;MaxLength = size;
          GetInfo()-&gt;Length = cl;
      }
  
      void Free()
      {
          if(Buffer)
          {
              delete[] (Buffer - sizeof(CStringInfo));
          }
      }
  
      CStringInfo* GetInfo()
      {
          return (CStringInfo*)(this-&gt;Buffer - sizeof(CStringInfo));
      }
  };
  </code></pre>
  
  <p>And code I test it on:</p>
  
  <pre><code>#include &lt;cstdio&gt;
  #include "CString.hpp"
  
  CString global_str = "global string!";
  
  int main(int argc, char* argv[])
  {
      CString str = "string";
      printf("Test: %s, %s\n", str, global_str);
      return 0;
  }
  </code></pre>
  
  <p>If I don't have a destructor in the class, then I can pass it to printf and it will work just like it should (as a C string). But when I add destructor, GCC produces following error:</p>
  
  <pre><code>error: cannot pass objects of non-trivially-copyable type 'class CString' through '...'
  </code></pre>
  
  <p>And in addition to that prior versions of GCC will give a warning + ud2 opcode.</p>
  
  <p>So... Question: can I actually make following construction work in GCC or is there any way, possibly not involving C varargs, to make something identical in use to above code?</p>