Description
It would be very nice if fields could be passed to initializers in C/C++.
currently, the constructors for the generated structs only accept an allocator.
I think that the generated constructors should instead be able to accept any of their fields, allowing for (designated) initializers to be used.
as an example, currently in order to send a message with a geographic_msgs/GeoPose, I will have to do something like this:
geographic_msgs::msg::GeoPose pose;
pose.position = /* ... */;
pose.orientation = /* ... */;
or, I personally prefer to use auto pose = new geographic_msgs::msg::GeoPose(); as even though it's slightly longer it makes the initialization more explicit.
the current approach has two aspects I dislike about it:
- I think it doesn't look as nice
- you cannot mark it as
const, because it needs to be modified after it's constructed.
I think it would be nice if instead of that one could write:
const auto pose = geograpgic_msgs::msg::GeoPose{
.position = /* ... */,
.orientation = /* ... */,
};
or
const geograpgic_msgs::msg::GeoPose pose = {
.position = /* ... */,
.orientation = /* ...*/,
};
I am not too familiar with C++ so I'm unsure if designated initializers can be used in conjunction with constructors, as a constructor is used for the allocator.
if they cannot be used together, then the other alternative is to provide a constructor with all the default values, so that at least the following can be done:
const auto pose = new geograpgic_msgs::msg::GeoPose(
/* ... */,
/* ... */
);
if constructors need to be used, then there are a couple of ways you could go about it:
- put fields in the order they appear in in the definition
- put fields without explicit defaults first and fields with explicit defaults afterwards (both in the order they appear in in the definition)
- generate a bunch of different constructor with every permutation of fields that have a default
Motivation
I think it would look nicer, and it would also allow declaring things as const.
Design / Implementation Considerations
No response
Additional Information
No response
Description
It would be very nice if fields could be passed to initializers in C/C++.
currently, the constructors for the generated structs only accept an allocator.
I think that the generated constructors should instead be able to accept any of their fields, allowing for (designated) initializers to be used.
as an example, currently in order to send a message with a
geographic_msgs/GeoPose, I will have to do something like this:or, I personally prefer to use
auto pose = new geographic_msgs::msg::GeoPose();as even though it's slightly longer it makes the initialization more explicit.the current approach has two aspects I dislike about it:
const, because it needs to be modified after it's constructed.I think it would be nice if instead of that one could write:
or
I am not too familiar with C++ so I'm unsure if designated initializers can be used in conjunction with constructors, as a constructor is used for the allocator.
if they cannot be used together, then the other alternative is to provide a constructor with all the default values, so that at least the following can be done:
if constructors need to be used, then there are a couple of ways you could go about it:
Motivation
I think it would look nicer, and it would also allow declaring things as
const.Design / Implementation Considerations
No response
Additional Information
No response