cereal::LoadAndConstruct&lt; T &gt;(3) Library Functions Manual cereal::LoadAndConstruct&lt; T &gt;(3)

# [NAME](#NAME)

cereal::LoadAndConstruct&lt; T &gt; - A class that allows cereal to load smart pointers to types that have no default constructor.

# [SYNOPSIS](#SYNOPSIS)

#include &lt;/home/build/include/cereal/access.hpp&gt;

# [Detailed Description](#Detailed_Description)

## [template&lt;class T&gt;](#template_class_T_)

struct cereal::LoadAndConstruct&lt; T &gt;"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**

*T* The type to specialize for

# [Author](#Author)

Generated automatically by Doxygen for cereal from the source code.

Sat Oct 10 2026 08:08:15 cereal

---

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