Boost logo

Boost :

From: Martin Wille (mw8329_at_[hidden])
Date: 2005-07-13 15:12:56


Peter Dimov wrote:
> Martin Wille wrote:
>
>
>>Usage example:
>>
>> // udt.hpp
>> struct udt { };
>> std::ostream &operator<<(std::ostream &, udt const &);
>> std::istream &operator>>(std::istream &, udt const &);
>>
>> // someclass.hpp
>> #include "udt.hpp"
>> #include <boost/serialization.hpp>
>>
>> struct someclass
>> {
>> template<typename Archive>
>> void serialize(Archive &ar, const unsigned int version)
>> {
>> ar & boost::serialization::lexical_cast<std::string>(my_udt);
>> }
>>
>> private:
>> udt my_udt;
>> };
>
>
> This puts the serialization support for udt at the wrong level. If you have
> twelve classes that have four udt members each, you'll have to repeat
> "boost::serialization::lexical_cast<std::string>()" 48 times. And
> vector<udt> will still not work.
>
> It's better to define load+save for udt.

This wasn't the case for my code, which used each udt only once.

> If you do that, you'll see that the proposed wrapper adds almost no value.
> The 'almost' part comes from track_never, which enables you to sidestep the
> usual problem of serializing temporaries. :-)

With the lexical_cast wrapper you can do:

template <typename Archive>
void serialize(Archive &ar, udt &my_udt, unsigned int version)
{
    ar & boost::serialization::lexical_cast<std::string>(my_udt);
}

IMHO, this is still better than spelling out the load/save functions.

Thanks for your comments,
m

Send instant messages to your online friends http://au.messenger.yahoo.com


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