Compiling tf or tf2 with -std=gnu++0x

Hello,

I am trying to rosmake a package (modular_cloud_matcher) which forces -std=gnu++0x compiler option. This results in the following error:

/opt/ros/electric/stacks/geometry/tf/include/tf/tf.h:91:44: error: ‘constexpr’ needed for in-class initialization of static data member ‘DEFAULT_CACHE_TIME’ of non-integral type


and a similar one in tf2.

A possible solution is to change in tf.h from

static const double DEFAULT_CACHE_TIME = 10.0;


to

static constexpr double DEFAULT_CACHE_TIME = 10.0;


I suppose, however, this is not the right way to solve the issue, because next update will remove my modification. I am rather new to the ROS community, so I would like to know what should be done in such cases.

edit retag close merge delete

Sort by » oldest newest most voted

When you find a compiler problem like this, please open a defect ticket for the appropriate ROS component (in this case geometry). Mention the exact system version and compiler (i.e. gcc --version) you use.

In this case, constexpr is a new feature of the recent C++11 standard, which is not supported for any current ROS distribution. As REP-0003 states:

• "Use of C++[0|1]x or tr1 features are only allowed if support for that feature is checked at compile time, and equivalent functionality exists without requiring C++[0|1]x code. A wholesale jump to C++[0|1]x will not happen until all commonly used OS platforms fully support it."

So, while you should submit your patch with the defect ticket, the full solution will need to be somewhat different, to support all the other ROS platforms.

more

Thanks for the suggestion. I opened a ticket at ros-pkg trac. Clearly, 'constexpr' is not an acceptable solution as it won't compile with C++03.
( 2011-11-20 08:12:58 -0500 )edit

Technically, the old C++ standard says that non-integer constant data is illegal in class declarations. The easy way to fix this is to define it in the source file. I fixed it by moving the definition of DEFAULT_CACHE_TIME to tf.cpp and it now works with GCC 4.4.5 on Debian 6.0.4, no --std argument needed. I'm pretty sure it will also work in C++0x. constexpr is more for template metaprogramming and places where you can't separate the definition from the declaration, or would like to make something constant for performance tuning.

more

Hello,

I am the maintainer of modular_cloud_matcher, and while I'm aware of ROS' repulsion of the use of C++0x, I decided to use it nevertheless because it made the code much cleaner and worked for the main ROS platforms starting from GCC 4.4 (Ubuntu Lucid). I did not use any features not explicitly supported and tested with GCC 4.4. Moreover, I think that it is good people begin testing ROS with C++0x as it will help with the eventual transition.

I did not expect too much problems such as this one to happen, as C++0x is supposed to be highly backward compatible, but it does happen sometimes.

I have created patches that fix these issues, see the related ticket

more