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

C++: Provide constructors for auto-generated unions #279

Open
pavel-kirienko opened this issue Feb 27, 2023 · 5 comments
Open

C++: Provide constructors for auto-generated unions #279

pavel-kirienko opened this issue Feb 27, 2023 · 5 comments
Labels
feature New feature or request

Comments

@pavel-kirienko
Copy link
Member

Instead of:

Ty_1 val;
val.foo = 123;
val.bar = baz;

go like:

const Ty_1 val(123, baz);

To facilitate immutability and URVO.

@pavel-kirienko pavel-kirienko added the feature New feature or request label Feb 27, 2023
@ASMfreaK
Copy link
Contributor

This partially works with brace-init, for everything except for unions.

// with C++17
const Heartbeat_1_0 hb{
  1244, // uptime
   {Health::NOMINAL},
   {Mode::WARNING},
   0x1
};

// with C++20 or extension
const Heartbeat_1_0 hb{
  .uptime=1244, // uptime
   .health={Health::NOMINAL},
   .mode={Mode::WARNING},
   .vssc=0x1
};

@pavel-kirienko
Copy link
Member Author

Indeed, the members are public. I guess I am going close this and walk away with shame.

@pavel-kirienko pavel-kirienko closed this as not planned Won't fix, can't repro, duplicate, stale Feb 27, 2023
@ASMfreaK
Copy link
Contributor

Unions would really need a constructor to initialize this way. We would need some kind of a tag dispatch to differentiate between different named variants.

// proposed syntax
const Value_1_0 val{Value_1_0::natural_16_tag, 1, 2, 3};

And variant construction is a problem right now. At the moment we can't constuct specific variant, we can only set value after:

Value_1_0 v;
v.set_natural_16({{10, 10, 0}});

@ASMfreaK ASMfreaK reopened this Feb 27, 2023
@pavel-kirienko pavel-kirienko changed the title C++: Provide telescoping constructors for auto-generated classes C++: Provide telescoping constructors for auto-generated unions Feb 27, 2023
@ASMfreaK
Copy link
Contributor

I think the issue exists, just not for simple POD values as listed earlier.

@ASMfreaK
Copy link
Contributor

There is also the issue of missing value init (as in the opening post)

@pavel-kirienko pavel-kirienko changed the title C++: Provide telescoping constructors for auto-generated unions C++: Provide constructors for auto-generated unions Mar 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants