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:
: 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

// correct shared_ptr serialization depends upon object tracking
// being used.
   != 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
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
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
tracking as a global side-effect for types which otherwise would expeced to
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, kalb at, bjorn.karlsson at, gregod at, wekempf at