.TH "cereal::virtual_base_class< Base >" 3 "Sat Aug 29 2026 00:42:30" "cereal" \" -*- nroff -*- .ad l .nh .SH NAME cereal::virtual_base_class< Base > \- Casts a derived class to its virtual base class in a way that allows cereal to track inheritance\&. .SH SYNOPSIS .br .PP .PP \fR#include \fP .PP Inherits \fBcereal::traits::detail::BaseCastBase\fP\&. .SS "Public Member Functions" .in +1c .ti -1c .RI "template \fBvirtual_base_class\fP (Derived const *derived)" .br .in -1c .SS "Public Attributes" .in +1c .ti -1c .RI "Base * \fBbase_ptr\fP" .br .in -1c .SH "Detailed Description" .PP .SS "template .br 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\&. .PP It is safe to use \fBvirtual_base_class\fP 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 \fBcereal::base_class<>\fP if you do not need to worry about virtual inheritance\&. .PP 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 (\fBcereal/types/polymorphic\&.hpp\fP)\&. .PP \fBSee also\fP .RS 4 \fBbase_class\fP .RE .PP .PP .nf struct MyBase { int x; template void serialize( Archive & ar ) { ar( x ); } }; struct MyLeft : virtual MyBase //<\-\- Note the virtual inheritance { int y; template void serialize( Archive & ar ) { ar( cereal::virtual_base_class( this ) ); ar( y ); } }; struct MyRight : virtual MyBase { int z; template void serialize( Archive & ar ) { ar( cereal::virtual_base_clas( this ) ); ar( z ); } }; // diamond virtual inheritance; contains one copy of each base class struct MyDerived : virtual MyLeft, virtual MyRight { int a; template void serialize( Archive & ar ) { ar( cereal::virtual_base_class( this ) ); // safely serialize data members in MyLeft ar( cereal::virtual_base_class( 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 }; } .fi .PP .SH "Author" .PP Generated automatically by Doxygen for cereal from the source code\&.