title: How to boost::serialize an std/boost::optional?
body: <p>How can I serialize a class (with <code>boost::serialization</code>) that contains a <code>boost::optional</code>?</p>
  
  <p>I.e. the following code will give an error when instantiated.</p>
  
  <blockquote>
    <p>error C2039: 'serialize' : is not a member of 'boost::optional' C:\boost\boost_1_55_0\boost\serialization\access.hpp  118</p>
  </blockquote>
  
  <pre><code>#include &lt;boost/archive/text_oarchive.hpp&gt;
  #include &lt;boost/archive/text_iarchive.hpp&gt;
  
  class MyClass {
  private:
      friend class boost::serialization::access;
  
      template&lt;class Archive&gt;
      void serialize(Archive &amp; ar, const unsigned int version)
      {
          ar &amp; my_member;
      }
  
      boost::optional&lt;int&gt; my_member;
  };
  
  int main() {
      std::ofstream ofs("filename.txt");
      const MyClass g;
      boost::archive::text_oarchive oa(ofs);
      oa &lt;&lt; g;
      return 0;
  }
  </code></pre>
  
  <p>I understand there's probably a deeper question involved (what should you write to the file when the value is not present?), but there must be some standard solution for it. I am looking for the most simple way to solve this.</p>