cereal::virtual_base_class< Base > Struct Template Reference

Casts a derived class to its virtual base class in a way that allows cereal to track inheritance. More...

`#include </home/shane/workspace/cereal/include/cereal/types/base_class.hpp>`

Inheritance diagram for cereal::virtual_base_class< Base >:

## Public Member Functions

template<class Derived >
| virtual_base_class (Derived const *derived)

## Public Attributes

Base * | base_ptr

## Detailed Description

### template<class Base>
struct cereal::virtual_base_class< Base >

Casts a derived class to its virtual base class in a way that allows cereal to track inheritance.

This should be used in cases when a derived type features virtual inheritance from some base type. This allows cereal to track the inheritance and to avoid making duplicate copies during serialization.

It is safe to use virtual_base_class in all circumstances for serializing base classes, even in cases where virtual inheritance does not take place, though it may be slightly faster to utilize cereal::base_class<> if you do not need to worry about virtual inheritance.

This also automatically registers polymorphic relation between the base and derived class, assuming they are indeed polymorphic. Note this is not the same as polymorphic type registration. For more information see the documentation on polymorphism. If using a polymorphic class, be sure to include support for polymorphism (cereal/types/polymorphic.hpp).

See also
  base_class

struct MyBase
{
int x;
template <class Archive>
void serialize( Archive & ar )
{
ar( x );
}
};
struct MyLeft : virtual MyBase //<-- Note the virtual inheritance
{
int y;
template <class Archive>
void serialize( Archive & ar )
{
ar( cereal::virtual_base_class<MyBase>( this ) );
ar( y );
}
};
struct MyRight : virtual MyBase
{
int z;
template <class Archive>
void serialize( Archive & ar )
{
ar( cereal::virtual_base_clas<MyBase>( this ) );
ar( z );
}
};
// diamond virtual inheritance; contains one copy of each base class
struct MyDerived : virtual MyLeft, virtual MyRight
{
int a;
template <class Archive>
void serialize( Archive & ar )
{
ar( cereal::virtual_base_class<MyLeft>( this ) ); // safely serialize data members in MyLeft
ar( cereal::virtual_base_class<MyRight>( this ) ); // safely serialize data members in MyRight
ar( a );
// Because we used virtual_base_class, cereal will ensure that only one instance of MyBase is
// serialized as we traverse the inheritance heirarchy. This means that there will be one copy
// each of the variables x, y, z, and a
// If we had chosen to use static_cast<> instead, cereal would perform no tracking and
// assume that every base class should be serialized (in this case leading to a duplicate
// serialization of MyBase due to diamond inheritance
};
}

The documentation for this struct was generated from the following file:

- /home/shane/workspace/cereal/include/cereal/types/base_class.hpp

cereal::serialize
void serialize(Archive &, std::less< T > &)
Saving for std::less.
Definition: functional.hpp:39
cereal::virtual_base_class
Casts a derived class to its virtual base class in a way that allows cereal to track inheritance.
Definition: base_class.hpp:188