From 4f3b6bbabe0a7f55f006d3e8ee1a1a7d887a716f Mon Sep 17 00:00:00 2001 From: eyalz800 Date: Mon, 24 Feb 2025 17:10:07 +0000 Subject: [PATCH] Updated readme --- README.md | 34 ++++++++++++++++++++++++++++- test/src/test_byte_serializable.cpp | 1 + 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8570ae7..0ec2214 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,8 @@ polymorphic types with fixed 8 bytes of sha1 serialization id. Introduction ------------ For many types, enabling serialization is transparent and requires no additional lines of code. -These types are required to be of aggregate type, with non array members. +Pre-C++26, these types are required to be of aggregate type, with non array members. +Post C++26, the feature "(P1061) Structured bindings can introduce a pack", lifts this requirement. Here is an example of a `person` class with name and age: ```cpp struct person @@ -185,6 +186,8 @@ which one you used: Serializing Non-Aggregates -------------------------- +Pre-C++26, the library provides a way to serialize non-aggregates, requiring the user to provide +the number of members in the class. For most non-aggregate types (or aggregate types with array members), enabling serialization is a one liner. Here is an example of a non-aggregate `person` class: @@ -220,6 +223,22 @@ auto serialize(const person & person) -> zpp::bits::members<2>; } // namespace my_namespace ``` +Post C++26, the above works without a need to provide the number of members: +```cpp +namespace my_namespace +{ +struct person +{ + person(auto && ...){/*...*/} // Make non-aggregate. + + std::string name; + int age{}; +}; + +} // namespace my_namespace +``` + +### Deprecated behavior In some compilers, *SFINAE* works with `requires expression` under `if constexpr` and `unevaluated lambda expression`. It means that even with non aggregate types the number of members can be detected automatically in cases where all members are in the same struct. To opt-in, define `ZPP_BITS_AUTODETECT_MEMBERS_MODE=1`. @@ -235,6 +254,7 @@ struct person ``` This works with `clang 13`, however the portability of this is not clear, since in `gcc` it does not work (it is a hard error) and it explicitly states in the standard that there is intent not to allow *SFINAE* in similar cases, so it is turned off by default. +Update: this is not supported anymore with recent versions of `clang`, and as such `ZPP_BITS_AUTODETECT_MEMBERS_MODE=1` is deprecated. Serializing Private Classes --------------------------- @@ -253,6 +273,18 @@ private: }; ``` +Post C++26, the above example is simplified to the below without a need to specify the number of members: +```cpp +struct private_person +{ + friend zpp::bits::access; + +private: + std::string name; + int age{}; +}; +``` + Explicit Serialization ---------------------- To enable save & load of any object using explicit serialization, which works diff --git a/test/src/test_byte_serializable.cpp b/test/src/test_byte_serializable.cpp index 317b83d..f3deb02 100644 --- a/test/src/test_byte_serializable.cpp +++ b/test/src/test_byte_serializable.cpp @@ -204,6 +204,7 @@ class inaccessible_requires_padding #if __cpp_structured_bindings < 202411L // This check is no longer supported until C++26 reflections. static_assert(!zpp::bits::concepts::byte_serializable< inaccessible_requires_padding>); +#endif class inaccessible_friend_requires_padding {