Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7623593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:44:43+00:00 2026-05-31T04:44:43+00:00

How would you serialize/deserialize this class using boost::serialization? #include <vector> struct Foo { struct

  • 0

How would you serialize/deserialize this class using boost::serialization?

#include <vector>

struct Foo {
    struct Bar {
        std::vector<int> * data; // Must point to Foo::data

        Bar( std::vector<int> * d ) : data(d) { }
    };

    std::vector<int> data;
    std::vector<Bar> elements;

    Foo() {
        // do very time consuming calculation to populate "data" and "elements"
    }
};

The constructor in Foo must not be executed when the objected is loaded from the serialized data, but if the object is default constructed the constructor must be evaluated.

It is okay to add a default constructor to Bar, but after serialization the Foo::Bar::data must point to the Foo::data.

EDIT: Following is a non-working implementation of my attempt

This is my attempt based on the hints from @Matthieu. The problem is that when I deserialize Foo, I get no elements in Foo::data and Foo::elements.

struct Foo {
    struct Bar {
        std::vector<int> * data;

        Bar( ) : data( 0 ) { }
        Bar( std::vector<int> * d ) : data(d) { }

        template<class Archive>
        void serialize(Archive & ar, const unsigned int version) {
            ar & data;
        }
    };

    std::vector<int> data;
    std::vector<Bar> elements;

    Foo() {
        std::cerr << "Running default constructor" << std::endl;
        data.push_back(1);
        data.push_back(2);
        data.push_back(3);
        data.push_back(4);
        data.push_back(5);
        elements.push_back( Bar( &data ) );
        elements.push_back( Bar( &data ) );
        elements.push_back( Bar( &data ) );
    }

    template<class Archive>
    Foo( Archive & ar ) {
        ar >> data; // is this corrent?
        ar >> elements;
    }

private:
    BOOST_SERIALIZATION_SPLIT_MEMBER();
    friend class boost::serialization::access;

    template<class Archive>
    void save(Archive & ar, const unsigned int version) const {
        const std::vector<int> * data_ptr = &data;

        // should data be seriliazed as pointer...
        // it is used as a pointer in Bar
        ar << data_ptr;
        ar << elements;
    }
};

int main(int argc, const char *argv[])
{
#if 0
    // serialize
    Foo foo;
    boost::archive::text_oarchive oar(std::cout);
    oar << foo;

#else
    // deserialize
    boost::archive::text_iarchive oar(std::cin);
    Foo foo(oar);

#endif
    std::cerr << foo.data.size() << std::endl;
    std::cerr << foo.elements.size() << std::endl;

    std::cerr << (&foo.data) << std::endl;
    for( const auto& a : foo.data )
        std::cerr << a << " ";
    std::cerr << std::endl;

    for( const auto& a : foo.elements)
        std::cerr << a.data << " ";
    std::cerr << std::endl;

    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T04:44:45+00:00Added an answer on May 31, 2026 at 4:44 am

    There is a section in the documentation that describes how to (de)serialize classes with non-default constructors. See here.

    Basically, you must implement two functions called save_construct_data and load_construct_data in the namespace boost::serialization to write out and read in the data used to construct instances of your class. You can then call a non-default constructor of Foo from the load_construct_data function with the parameters necessary to reconstruct a Foo object.


    Here is a working example based on your updated code:

    Note that I’ve used shared_ptr‘s to clarify that the data member serialized by Foo and Bar are referencing the same thing.

    #include <vector>
    #include <boost/archive/text_oarchive.hpp>
    #include <boost/archive/text_iarchive.hpp>
    #include <boost/serialization/vector.hpp>
    #include <boost/serialization/shared_ptr.hpp>
    #include <boost/serialization/scoped_ptr.hpp>
    #include <boost/shared_ptr.hpp>
    #include <iostream>
    #include <sstream>
    
    struct Foo {
        struct Bar {
            boost::shared_ptr< std::vector<int> > data; // Must point to Foo::data
    
            Bar( boost::shared_ptr< std::vector<int> > d ) : data(d) { }
    
            template<class Archive>
            void serialize(Archive & ar, const unsigned int version)
            {
              // ** note that this is empty **
            }
        };
    
        boost::shared_ptr< std::vector<int> > data;
        std::vector<Bar> elements;
    
        Foo() : data( new std::vector<int>() ) {
            std::cerr << "Running default constructor" << std::endl;
            data->push_back(1);
            data->push_back(2);
            data->push_back(3);
            data->push_back(4);
            data->push_back(5);
            elements.push_back( Bar( data ) );
            elements.push_back( Bar( data ) );
            elements.push_back( Bar( data ) );
        }
    
        template<class Archive>
        void serialize(Archive & ar, const unsigned int version)
        {
          // ** note that this is empty **
        }
    
        Foo(
          boost::shared_ptr< std::vector<int> > const & data_,
          std::vector<Bar> const & elements_ ) : data( data_ ), elements( elements_ )
        {
            std::cout << "cheap construction" << std::endl;
        }
    };
    
    namespace boost { namespace serialization {
    
    template<class Archive>
    inline void save_construct_data(
        Archive & ar, const Foo * foo, const unsigned int file_version
    ){
        ar << foo->data << foo->elements;
    }
    
    template<class Archive>
    inline void load_construct_data(
        Archive & ar, Foo * foo, const unsigned int file_version
    ){
        boost::shared_ptr< std::vector<int> > data;
        std::vector<Foo::Bar> elements;
    
        ar >> data >> elements;
    
        ::new(foo)Foo(data, elements);
    }
    
    template<class Archive>
    inline void save_construct_data(
        Archive & ar, const Foo::Bar * bar, const unsigned int file_version
    ){
        ar << bar->data;
    }
    
    template<class Archive>
    inline void load_construct_data(
        Archive & ar, Foo::Bar * bar, const unsigned int file_version
    ){
        boost::shared_ptr< std::vector<int> > data;
    
        ar >> data;
    
        ::new(bar)Foo::Bar(data);
    }
    
    }}
    
    int main()
    {
      std::stringstream ss;
    
      {
        boost::scoped_ptr< Foo > foo( new Foo() );
    
        std::cout << "size before serialization is: " << foo->data->size() << std::endl;
    
        boost::archive::text_oarchive oa(ss);
        oa << foo;
      }
    
      {
        boost::scoped_ptr< Foo > foo;
    
        boost::archive::text_iarchive is(ss);
        is >> foo;
    
        std::cout << "size after deserialization is: " << foo->data->size() << std::endl;
      }
    
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that I serialize/deserialize using XmlSerializer . This class contains a
I would like to serialize and deserialize objects without having to worry about the
instead of using getParameterByName('Field', PostData) (PostData == $('form').serialize();) I would like to write PostData.Field,
I am trying to write a class to serialize/deserialize something as simple as: <table
I am using binary serialization (with BinaryFormatter, etc) to serialize a graph of objects.
I'm trying to teach-myself how to serialize/deserialize to/from XML using the attribute-based serializer. I've
I am having an issue using BinaryFormatter.Serialize. I have this generic extension method to
I know that you can use boost serialization to serialize to a text format
I have a class PersonList [XmlRoot(Persons)] PersonList : List<Human> when I serialize this to
I have a question today involving the StreamReader class. Specifically initializing this class using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.