I'd like to know if there is a way to enumerate over the available constants defined in a message. As an example, let's take the visualization_msgs/Marker.msg as an example. This message has a list of constants that map to various inbuilt marker types. I would like to be able to iterate over these predefined constants in order to validate that a user provided choice is indeed a valid option.

Similar to the ROS2 marker's type, I have a custom message type with predefined constants. I would like the user to be able to indicate their desired constant in the params file which I then use in conjunction with the custom type. Before any of this though, I would first like to validate the parameter provided by the user to ensure that it is in fact meaningful as per the message type.

I haven't been able to find much information other than that these constants have been defined internally as static constexpr uint8_t type_xyz within the struct. If there is a way to iterate over all of them, I would love to know.

Thank you!


1 Answer 1


Unfortunately I don't think it is possible to do this automatically given the way the IDL is defined. You'll notice in the marker message there are actually two sets of constants (one for markers, one for actions. Thus, even if you had a way of using reflection to iterate through all constexpr values, you'd have to parse out which values belong together.

Best to write your validation code by hand with unit tests to protect your code in case your values change.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.