From 03cc83a5440d5c003cda952df5b9badbf8b0589f Mon Sep 17 00:00:00 2001 From: eyalz800 Date: Mon, 1 Apr 2024 22:11:26 +0300 Subject: [PATCH] Variant of private default constructible fix invalid return --- test/src/test_variant.cpp | 28 ++++++++++++++++++++++++++++ zpp_bits.h | 1 + 2 files changed, 29 insertions(+) diff --git a/test/src/test_variant.cpp b/test/src/test_variant.cpp index bb595e2..b31f030 100644 --- a/test/src/test_variant.cpp +++ b/test/src/test_variant.cpp @@ -38,6 +38,34 @@ TEST(variant, string) EXPECT_EQ(std::get(v), "1234"); } +TEST(variant, private_default_constructible) +{ + struct int_string : std::string + { + int_string(int i) : std::string(std::to_string(i)) + { + } + + private: + friend zpp::bits::access; + int_string() = default; + }; + + auto [data, in, out] = zpp::bits::data_in_out(); + out(std::variant(1234)).or_throw(); + + EXPECT_EQ(encode_hex(data), + "01" + "04000000" + "31323334"); + + std::variant v; + in(v).or_throw(); + + EXPECT_TRUE(std::holds_alternative(v)); + EXPECT_EQ(std::get(v), "1234"); +} + namespace string_versioning::v1 { struct person diff --git a/zpp_bits.h b/zpp_bits.h index cb651ed..79f9fb0 100644 --- a/zpp_bits.h +++ b/zpp_bits.h @@ -2993,6 +2993,7 @@ class in return result; } variant = std::move(*object); + return errc{}; } }...};