An alternative implementation of the smart pointers in the standard header <memory> with some additional features.
Blog post about this library and the motivation behind it.
xmem requires at least C++17.
unique_ptr: no known differencesshared_ptr:- xmem allows the control block type to be set as a template argument, thus allowing various features by control-block-level polymorphism.
- Most notably (and the main motivation behind the lib): tracking of strong or weak refs, which allow dealing with
shared_ptr-based leaks - Non-atomic refcounts which improve performance as long as you only use the pointers in a single thread (implemented in
xmem::local_shared_ptr)
- Most notably (and the main motivation behind the lib): tracking of strong or weak refs, which allow dealing with
- In the spirit of the deprecated atomic operations on
std::shared_ptrin C++20, xmem offers no atomic ops onshared_ptr. It introduces the classatomic_shared_ptr_storageto take care of this need. - The owner (control block) of the pointer is directly accessible as
const void*throughptr.owner()and stronly typed asconst control_block_type*throughptr.t_owner() - There is no constructor through weak ptr, and no
shared_ptroperation throws an exception (except ones by proxy, on allocation or if constructing the object inmake_sharedthrows) - A helper function:
make_shared_ptrto make ashared_ptrfrom an existing object - A helper function:
make_aliasedto make ashared_ptrby aliasing another, but safely returningnullptrif the source is null.
- xmem allows the control block type to be set as a template argument, thus allowing various features by control-block-level polymorphism.
weak_ptr:- Like
shared_ptrit has the control block as a template argument and offers control block access throughownerandt_owner - The pointer has a boolean interface which means no associated control block and says nothing about whether the pointer has expired or not.
- An aliasing constructor like the one in
shared_ptris provided
- Like
- Besides
enable_shared_from_thisa non-template alternative is introducedenable_shared_fromwhich (in the author's opinion) has a better interface. This is inspired from Boost.SmartPtr. - Helper functions
same_owner- check whether two shared/weak pointers have the same ownerno_owner- check if a weak/shared pointer has no owner
- Functions from C++20:
make_shared_for_overwrite,make_unique_for_overwrite
The external functionalities: make_aliased, make_ptr, enable_shared_from, atomic_shared_ptr_storage, same_owner, no_owner, are also available for the applicable std:: pointers through the header xmem/std_helpers.hpp in namespace xstd.
This software is distributed under the MIT Software License.
See accompanying file LICENSE or copy here.
Copyright © 2022-2026 Borislav Stanimirov