From 361d7726fd4d42f18833eeeee5c26679641695fe Mon Sep 17 00:00:00 2001 From: Gennaro Prota Date: Thu, 6 Aug 2026 14:37:40 +0200 Subject: [PATCH] Serialize strings with an allocator of their own The archives implement primitives for `std::string` and `std::wstring` only, so a `basic_string` with an allocator of its own, `std::pmr::string` being the usual case, could be serialized only by declaring it `primitive_type`. That declaration states the archive already handles the type, which is untrue here, and the generic fallbacks silently truncated the string at the first whitespace or, for the binary archives, copied its bytes. Such a string now borrows the primitive of the correspondingly built default allocated string, so it needs no declaration at all. The name passed to `make_nvp` is null, which keeps the representation identical to that of `std::string` in every archive, xml included, and lets the two interoperate. Fixes #267. --- doc/traits.html | 16 +++ include/boost/serialization/string.hpp | 77 +++++++++++ test/Jamfile.v2 | 1 + test/test_string_allocator.cpp | 177 +++++++++++++++++++++++++ 4 files changed, 271 insertions(+) create mode 100644 test/test_string_allocator.cpp diff --git a/doc/traits.html b/doc/traits.html index b00765dc4..474b7f1a3 100644 --- a/doc/traits.html +++ b/doc/traits.html @@ -145,6 +145,22 @@

Implementation Level


 BOOST_CLASS_IMPLEMENTATION(my_class, boost::serialization::object_class_info)
 
+Take care when assigning primitive_type. +It states that the archive already handles the type itself; it is not a request +to make it do so. Archives implement primitives for the fundamental types and +for std::string and +std::wstring. For any other type they +fall back to stream extraction, in the text and xml archives, or to a copy of +sizeof(T) raw bytes, in the binary +ones. Assigning the level to a type an archive knows nothing about therefore +compiles, but reads back a wrong value rather than failing: declared this way, +a std::pmr::string stops at the first +space. A string whose allocator is not +std::allocator needs no such +declaration, as +string.hpp +serializes it just like std::string. +

If implementation level is not explicitly assigned, the system uses a default according to the following rules.