title: writing Binary File template functions
body: <p>I have made a few functions to write and read class to binary file.</p>
  
  <p>The 1st <code>writeElement</code> function i want to just be called if the argument is a class.</p>
  
  <p>Then i want to have the other functions for <code>int, double, size_t</code> in one function.
  There has to be a better way to do this, i shouldn't have to create a new function for all different types.</p>
  
  <pre><code>template &lt;class T&gt;
  inline void writeElement(ostream&amp; out, T target) { target.write(out); }
  inline void writeElement(ostream&amp; out, int target) { out.write((char*)&amp;target, sizeof(int)); }
  inline void writeElement(ostream&amp; out, double target) { out.write((char*)&amp;target, sizeof(double)); }
  inline void writeElement(ostream&amp; out, size_t target) { out.write((char*)&amp;target, sizeof(size_t)); }
  inline void writeElement(ostream&amp; out, const string str)
  {
      size_t size = str.size();
      writeElement(out, size);
      out.write(&amp;str[0], size);
  }
  template &lt;class T&gt;
  inline void writeElement(ostream&amp; out, vector&lt;T&gt; vector)
  {
      size_t size = vector.size();
      writeElement(out, size);
  
      for (auto &amp;element : vector)
      {
          writeElement(out, element);
      }
  }
  
  
  class Header
  {
  public:
      string sig;
      double  version;
  
  public:
      void read(istream&amp; in)
      {
          readElement(in, sig);
          readElement(in, version);
      }
  
      void write(ostream&amp; out)
      {
          writeElement(out, sig);
          writeElement(out, version);
      }
  };
  </code></pre>