| cereal::LoadAndConstruct< T >(3) | Library Functions Manual | cereal::LoadAndConstruct< T >(3) |
NAME
cereal::LoadAndConstruct< T > - A class that allows cereal to load smart pointers to types that have no default constructor.
SYNOPSIS
#include </home/build/include/cereal/access.hpp>
Detailed Description
template<class T>
struct cereal::LoadAndConstruct< T >"A class that allows cereal to load smart pointers to types that have no default constructor.
If your class does not have a default constructor, cereal will not be able to load any smart pointers to it unless you overload LoadAndConstruct for your class, and provide an appropriate load_and_construct method. You can also choose to define a member static function instead of specializing this class.
The specialization of LoadAndConstruct must be placed within the cereal namespace:
struct MyType
{
MyType( int x ); // note: no default ctor
int myX;
// Define a serialize or load/save pair as you normally would
template <class Archive>
void serialize( Archive & ar )
{
ar( myX );
}
};
// Provide a specialization for LoadAndConstruct for your type
namespace cereal
{
template <> struct LoadAndConstruct<MyType>
{
// load_and_construct will be passed the archive that you will be loading
// from as well as a construct object which you can use as if it were the
// constructor for your type. cereal will handle all memory management for you.
template <class Archive>
static void load_and_construct( Archive & ar, cereal::construct<MyType> & construct )
{
int x;
ar( x );
construct( x );
}
// if you require versioning, simply add a const std::uint32_t as the final parameter, e.g.:
// load_and_construct( Archive & ar, cereal::construct<MyType> & construct, std::uint32_t const version )
};
} // end namespace cereal
Please note that just as in using external serialization functions, you cannot get access to non-public members of your class by befriending cereal::access. If you have the ability to modify the class you wish to serialize, it is recommended that you use member serialize functions and a static member load_and_construct function.
load_and_construct functions, regardless of whether they are static members of your class or whether you create one in the LoadAndConstruct specialization, have the following signature:
// generally Archive will be templated, but it can be specific if desired template <class Archive> static void load_and_construct( Archive & ar, cereal::construct<MyType> & construct ); // with an optional last parameter specifying the version: const std::uint32_t version
Versioning behaves the same way as it does for standard serialization functions.
Template Parameters
Author
Generated automatically by Doxygen for cereal from the source code.
| Sat Aug 29 2026 00:42:30 | cereal |