Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove two-phase initialization for union types #557

Open
MikeLankamp-TomTom opened this issue Dec 14, 2023 · 2 comments
Open

Remove two-phase initialization for union types #557

MikeLankamp-TomTom opened this issue Dec 14, 2023 · 2 comments
Labels
c++ C++ language generator enhancement New feature or request
Milestone

Comments

@MikeLankamp-TomTom
Copy link

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.

@Mi-La
Copy link
Contributor

Mi-La commented Dec 15, 2023

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.

@Mi-La Mi-La added enhancement New feature or request c++ C++ language generator labels Dec 15, 2023
@Mi-La Mi-La added this to the Backlog milestone Dec 15, 2023
@Mi-La
Copy link
Contributor

Mi-La commented Dec 15, 2023

After discussion in #555 and regarding to #444 it seems that strong typedefs will fit better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ C++ language generator enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants