w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
How can I overload serialize boost functiokn to have one for file storing and another for tcp messages?

You can create your own Archive classes and then specialize the serialize() function template for your classes:

void serialize(MyBriefOutputArchive& ar, const unsigned int version) {
  // Alternative serialization.

void serialize(MyBriefInputArchive& ar, const unsigned int version) {
  // Alternative deserialization.

To make your own archive classes, you can subclass or copy the Boost classes so they otherwise work identically. Be careful because some archive classes, like boost::binary_oarchive and boost::binary_iarchive, have header comments that say they shouldn't be subclassed - you should copy their implementation instead (which is simple as all the work is done in their superclass).

If you use some template meta-programming tricks, you can recognize your own archive class within the generic serialize() without the need for specializations. This would be preferred under the DRY principle, but it might be easier to get things working with specializations first.

© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.