You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Union types are turned into Any types in C++ (basically std::any). However, these must be constructed via two-phase initialization:
union SimpleUnion
{
uint8 value8;
uint16 value16;
};
becomes
SimpleUnion val;
val.setValue8(42);
It is more inline with modern C++ practices to allow inline creation of these unions, for instance via any of the following forms:
const auto val = SimpleUnion(SimpleUnion::value8{}, 42); // tag-based construction
const auto val = SimpleUnion::create_value8(42); // static factory method
const auto val = SimpleUnion(SimpleUnion::value8{42}); // strong typedef
Similarly, zserio could adopt std::any's approach fully and also support accessing it via e.g. val.get<SimpleUnion::value8>();, which combines well with the tag-based or strong typedef approach above.
The text was updated successfully, but these errors were encountered:
Thanks for posting the issue. Current solution was chosen for simplicity and it should be possible to improve unions and probably also choices constructors. I like the tag-based solution most since we already use some tags elsewhere.
We can consider also the generic getter with tags.
We should consider how to deal with compound types members - whether to support only perfect forwarding constructors or also some kind of inplace constructor.
Union types are turned into Any types in C++ (basically std::any). However, these must be constructed via two-phase initialization:
becomes
It is more inline with modern C++ practices to allow inline creation of these unions, for instance via any of the following forms:
Similarly, zserio could adopt std::any's approach fully and also support accessing it via e.g.
val.get<SimpleUnion::value8>();
, which combines well with the tag-based or strong typedef approach above.The text was updated successfully, but these errors were encountered: