rosidl icon indicating copy to clipboard operation
rosidl copied to clipboard

[rosidl_generator_cpp] Generate constructors that take all msg members as arguments

Open jmachowinski opened this issue 1 year ago • 4 comments

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

jmachowinski avatar Nov 26 '24 10:11 jmachowinski

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).

alsora avatar Dec 08 '24 19:12 alsora

@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.

jmachowinski avatar Dec 17 '24 17:12 jmachowinski

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?

fujitatomoya avatar Dec 17 '24 18:12 fujitatomoya

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.

jmachowinski avatar Dec 17 '24 18:12 jmachowinski