roscpp overview: Initialization and Shutdown | Basics | Advanced: Traits [ROS C Turtle] | Advanced: Custom Allocators [ROS C Turtle] | Advanced: Serialization and Adapting Types [ROS C Turtle] | Publishers and Subscribers | Services | Parameter Server | Timers (Periodic Callbacks) | NodeHandles | Callbacks and Spinning | Logging | Names and Node Information | Time | Exceptions | Compilation Options | Advanced: Internals | tf/Overview | tf/Tutorials | C++ Style Guide

Message Traits

New in C Turtle

Message traits (and the message_traits namespace) allow you to query certain information about a message. They are mainly used to allow adapting of C++ types to ROS msg types, but can also be useful for retrieving information such as the datatype, md5sum or full message definition from a message.

All traits must be defined in the namespace ros::message_traits

Required traits for advertise/subscribe

   1 template<typename M>
   2 struct MD5Sum
   3 {
   4   static const char* value();
   5   static const char* value(const M& m);
   6 };
   8 template<typename M>
   9 struct DataType
  10 {
  11   static const char* value();
  12   static const char* value(const M& m);
  13 };
  15 template<typename M>
  16 struct Definition
  17 {
  18   static const char* value();
  19   static const char* value(const M& m);
  20 };

For backwards compatibility, these default to returning the values of e.g. __s_getMD5Sum()/__getMD5Sum().

Optional Traits

template<typename M> struct IsSimple;

  • A "simple" message is one which, given a vector/array of them, can be memcpy'd directly. Note that due to the padding rules of various compilers you need to be very careful about marking a struct as simple. For simple struct A, A must be a POD type and sizeof(A) must equal the sum of the length of serialization for each of its members.

template<typename M> struct IsFixedSize;

  • A fixed-size message is one which is always the same size. Used to optimize determining the size of a vector/array of M.

template<typename M> struct HasHeader;

  • Tells whether or not M has a header.

To change this to true you simply specialize for your message and inherit from TrueType instead of FalseType.

   1 template<typename M>
   2 struct Header
   3 {
   4   static roslib::Header* pointer(M& m);
   5   static roslib::Header const* pointer(const M& m);
   6 };
  • Returns a pointer to the header in the message. By default this returns &m.header if HasHeader<M>::value is true, or NULL if false.

   1 template<typename M>
   2 struct FrameId
   3 {
   4   static std::string* pointer(M& m);
   5   static std::string const* pointer(const M& m);
   6   static std::string value(const M& m);
   7 };
  • Returns the frame id of the message, as pointer or value type as requested.

   1 template<typename M>
   2 struct TimeStamp
   3 {
   4   static ros::Time* pointer(M& m);
   5   static ros::Time const* pointer(const M& m);
   6   static ros::Time value(const M& m);
   7 };
  • Returns the timestamp of the message, as pointer or value type as requested.

IsSimple, IsFixedSize and HasHeader can be used at compile time. Their default implementation is:

   1 template<typename M> struct IsSimple : public FalseType {};

FalseType/TrueType provide a bool integral constant value and a type typedef to support use with boost mpl and enable_if

  • FalseType::value = false

  • FalseType::type = FalseType

  • TrueType::value = true

  • TrueType::type = TrueType

Function Accessors


Return Value



md5sum<M>(const M& m)




datatype<M>(const M& m)




definition<M>(const M& m)








header<M>(M& m)


frameId<M>(M& m)


timeStamp<M>(M& m)


Wiki: roscpp/Overview/MessagesTraits (last edited 2011-03-24 05:41:33 by KenConley)