rosidl
rosidl copied to clipboard
[rosidl_generator_cpp] Generate constructors that take all msg members as arguments
Feature request
Feature description
It would be nice, if for every msg a constructor would be generated, that takes all msg members as arguments. There would be some positive effects from this:
- Compile time check if msg was modified
- Makes it harder to 'forget' to initialize a member
- Enforces RVO
Implementation considerations
Would slightly raise compile times Default constructors must stay for compability
FYI, see this old discussion on the topic: https://github.com/ros2/rosidl/issues/749 The purpose was different though.
In that case, we wanted to avoid to pay the cost for a default initialization. The solution we discussed in that case was based on the use of aggregate initialization with designated initializers, which requires C++ 20 (currently ROS uses C++ 17).
@clalancette My college had a good point to add to the discussion: Using constructors with arguments is 'The C++ way' of initializing objects. Not supporting it is kind of odd.
I do not have any objections for this feature. I do not quite follow the discussion during today's PMC meeting, what could be the downside of this feature?
There were two points against it:
- Adding yet another method to initialize a message.
- You would have a silent error if you would switch two members in your message of the same type.