Boost logo

Boost :

Subject: Re: [boost] [Serialization] serialize from and to an alreadyinstantiated object.
From: Robert Ramey (ramey_at_[hidden])
Date: 2011-06-10 12:40:32


Nicolas Guillot wrote:
> Robert Ramey <ramey <at> rrsd.com> writes:
> One question remains: how to deserialize onto an already instantiated
> object?

serialization is always done into an already instantiated object.
For pointers, there is a preliminary step of creating a new object,
but after that its the same always.

Or: how to implement the Load function better than:
>
> template<class T>
> void FileLoad(const std::string & path, T & t){
> std::ifstream ifs(path.c_str());
> T* p;
> ia >> p;
> t = *p;
> delete p;
> }

use::

template<class T>
void FileLoad(const std::string & path, T & t){
    std::ifstream ifs(path.c_str());
    boost::text_iarchive ia(ifs);
    ia >> t;
}

which will run a maximum possible efficiency and
will be exception safe.

Robert Ramey


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk