Boost logo

Boost Users :

From: Robert Ramey (ramey_at_[hidden])
Date: 2006-01-17 20:42:53


I compiled the following program on my system

#include <fstream>
#include <boost/archive/text_oarchive.hpp>
#include <boost/serialization/shared_ptr.hpp>
int main() {
    std::ofstream ofs("archive");
    boost::archive::text_oarchive oa(ofs);
    const boost::shared_ptr<int> i;
    oa << i;
    return 0;
}

The first syntax error I got was:
test_zmisc.cpp
c:\BoostMainCVS\libs\serialization\vc7ide\..\..\..\boost\serialization\shared_ptr.hpp(231)
: error C2027: use of undefined type 'boost::STATIC_ASSERTION_FAILURE<x>'
...

In my vc 7.1 system I double click on this error and I am displayed the
following source line from
boost/serialization/shared_ptr.hpp

// correct shared_ptr serialization depends upon object tracking
// being used.
BOOST_STATIC_ASSERT(
    boost::serialization::tracking_level<T>::value
   != boost::serialization::track_never
);
....

This indicates the the serialization of shared_ptr<T> is only implemented if
T is a tracked type.

So if you want to do this, there are a couple of options

a) alter shared_ptr<T> to handle untracked types. This is probably not a
great ideas
as usually untracked types are untracked for a good reason. There are other
reasons
as well but we needn't go into them here.

b) use BOOST_STRONG_TYPE to make an integer type that is tracked. This is
what
I recommend as its an unusual case an altering shared pointer serialization
would be
a pain in the neck and could easily have unexpected side-effects by
implementing
tracking as a global side-effect for types which otherwise would expeced to
be
untracked (like int)

So, using b) Your program looks like:

#include <fstream>
#include <boost/archive/text_oarchive.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/strong_typedef.hpp>

// defined a "special kind of integer"
BOOST_STRONG_TYPEDEF(int, tracked_int)
// define serialization for a tracked int
template<class Archive>
void serialize(Archive &ar, tracked_int & ti, const unsigned int version){
    // serialize the underlying int
    ar & static_cast<int &>(ti);
}
int main() {
    std::ofstream ofs("archive");
    boost::archive::text_oarchive oa(ofs);
    const boost::shared_ptr<tracked_int> i;
    oa << i;
    return 0;
}

This program does in fact compile on my vc 7.1 system.

I hope that answers your question.

Good Luck

Robert Ramey

Joseph Turian wrote:
>> Try the following change - If this does it - read the rationale
>> about using const with serialization.
>
> Unfortunately this doesn't change the compiler errors output at all.
> Can you duplicate this bad behavior on your system?
>
> Joseph


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net