OS Manuals

cereal_virtual_base_class(3)

cereal::virtual_base_class< Base > - Casts a derived class to its virtual base class in a way that allows cereal to track inheritance.

cereal::virtual_base_class< Base >(3) Library Functions Manual cereal::virtual_base_class< Base >(3)

cereal::virtual_base_class< Base > - Casts a derived class to its virtual base class in a way that allows cereal to track inheritance.

#include </home/build/include/cereal/types/base_class.hpp>

Inherits cereal::traits::detail::BaseCastBase.


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


Base * base_ptr

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
}; }

Generated automatically by Doxygen for cereal from the source code.

Sat Oct 10 2026 08:08:15 cereal

cereal 1.3.2-r6 · enterprise-packages · ingested 2026-10-10