cereal::memory\_detail::EnableSharedStateHelper&lt; T &gt;(3) Library Functions Manual cereal::memory\_detail::EnableSharedStateHelper&lt; T &gt;(3)

# [NAME](#NAME)

cereal::memory\_detail::EnableSharedStateHelper&lt; T &gt;

# [SYNOPSIS](#SYNOPSIS)

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

## [Public Member Functions](#Public_Member_Functions)

**EnableSharedStateHelper** (T \*ptr)  
Saves the state of some type inheriting from enable\_shared\_from\_this. void **restore** ()  
Restores the state of the held pointer (can only be done once). **~EnableSharedStateHelper** ()  
Restores the state of the held pointer if not done previously.

# [Detailed Description](#Detailed_Description)

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

class cereal::memory\_detail::EnableSharedStateHelper&lt; T &gt;"A helper struct for saving and restoring the state of types that derive from std::enable\_shared\_from\_this

This special struct is necessary because when a user uses load\_and\_construct, the weak\_ptr (or whatever implementation defined variant) that allows enable\_shared\_from\_this to function correctly will not be initialized properly.

This internal weak\_ptr can also be modified by the shared\_ptr that is created during the serialization of a polymorphic pointer, where cereal creates a wrapper shared\_ptr out of a void pointer to the real data.

In the case of load\_and\_construct, this happens because it is the allocation of shared\_ptr that perform this initialization, which we let happen on a buffer of memory (aligned\_storage). This buffer is then used for placement new later on, effectively overwriting any initialized weak\_ptr with a default initialized one, eventually leading to issues when the user calls shared\_from\_this.

To get around these issues, we will store the memory for the enable\_shared\_from\_this portion of the class and replace it after whatever happens to modify it (e.g. the user performing construction or the wrapper shared\_ptr in saving).

Note that this goes into undefined behavior territory, but as of the initial writing of this, all standard library implementations of std::enable\_shared\_from\_this are compatible with this memory manipulation. It is entirely possible that this may someday break or may not work with convoluted use cases.

Example usage:

```
T * myActualPointer;
{


  EnableSharedStateHelper<T> helper( myActualPointer ); // save the state


  std::shared_ptr<T> myPtr( myActualPointer ); // modifies the internal weak_ptr


  // helper restores state when it goes out of scope
}
```

When possible, this is designed to be used in an RAII fashion - it will save state on construction and restore it on destruction. The restore can be done at an earlier time (e.g. after construct() is called in load\_and\_construct) in which case the destructor will do nothing. Performing the restore immediately following construct() allows a user to call shared\_from\_this within their load\_and\_construct function.

**Template Parameters**

*T* Type pointed to by shared\_ptr

# [Constructor & Destructor Documentation](#Constructor_&_Destructor_Documentation)

## [template&lt;class T&gt; **cereal::memory\_detail::EnableSharedStateHelper**&lt; T &gt;::EnableSharedStateHelper (T * ptr) \[inline\]](#template_class_T__)

Saves the state of some type inheriting from enable\_shared\_from\_this.

**Parameters**

*ptr* The raw pointer held by the shared\_ptr

# [Author](#Author)

Generated automatically by Doxygen for cereal from the source code.

Mon Oct 26 2026 02:55:37 cereal

---

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