From cbf30026ef0dd7d785f51db5c1aa5891a0b92024 Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Wed, 7 Feb 2024 17:50:39 +0100 Subject: codec: Fixed union initialization when ambiguous cases occur --- modules/codec/c++/data.hpp | 2 +- modules/codec/tests/codec.cpp | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) (limited to 'modules') diff --git a/modules/codec/c++/data.hpp b/modules/codec/c++/data.hpp index 3fe7494..ca976d5 100644 --- a/modules/codec/c++/data.hpp +++ b/modules/codec/c++/data.hpp @@ -98,7 +98,7 @@ public: template data::value, T...>::type, encode::Native>& init(){ - value_ = data::value, T...>::type, encode::Native>{}; + value_.template emplace::value>(); return get(); } diff --git a/modules/codec/tests/codec.cpp b/modules/codec/tests/codec.cpp index b9ac4ea..05899cc 100644 --- a/modules/codec/tests/codec.cpp +++ b/modules/codec/tests/codec.cpp @@ -24,6 +24,11 @@ using TestUnion = Union< Member >; +using TestSameTypeUnion = Union< + Member, + Member +>; + using TestTuple = Tuple< TwoDimArray, UInt64 @@ -238,6 +243,19 @@ SAW_TEST("KelSimple Struct write and read back"){ SAW_EXPECT(native.template get<"number">().get() == 410, "Incorrect Decoding in number"); } +SAW_TEST("Native Union same type compilation"){ + using namespace saw; + + data native; + + native.template init<"two">().set(50u); + + SAW_EXPECT(!native.template holds_alternative<"one">(), "Two should be initialized"); + SAW_EXPECT(native.template holds_alternative<"two">(), "Two should be initialized"); + + SAW_EXPECT(native.template get<"two">().get() == 50u, "Should be 50"); +} + SAW_TEST("KelSimple Union write and read back"){ using namespace saw; -- cgit v1.2.3