cereal::virtual\_base\_class&lt; Base &gt;(3) Library Functions Manual cereal::virtual\_base\_class&lt; Base &gt;(3)

# [NAME](#NAME)

cereal::virtual\_base\_class&lt; Base &gt; - Casts a derived class to its virtual base class in a way that allows cereal to track inheritance.

# [SYNOPSIS](#SYNOPSIS)

#include &lt;/home/build/include/cereal/types/base\_class.hpp&gt;

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

## [Public Member Functions](#Public_Member_Functions)

template&lt;class Derived&gt; **virtual\_base\_class** (Derived const \*derived)

## [Public Attributes](#Public_Attributes)

Base * **base\_ptr**

# [Detailed Description](#Detailed_Description)

## [template&lt;class Base&gt;](#template_class_Base_)

struct cereal::virtual\_base\_class&lt; Base &gt;"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&lt;&gt;** 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


 };
}
```

# [Author](#Author)

Generated automatically by Doxygen for cereal from the source code.

Mon Oct 26 2026 02:55:37 cereal

---

Document: cereal_virtual_base_class(3) from cereal 1.3.2-r7 (Chainguard OS)
Source: https://github.com/USCiLab/cereal
