Skip to content

Commit

Permalink
Fix correct use of if constexpr()
Browse files Browse the repository at this point in the history
Closes #1270
  • Loading branch information
jlblancoc committed Oct 25, 2023
1 parent 814650b commit 4b6e108
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions libs/ros2bridge/src/range.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,26 @@ struct has_variance<T, std::void_t<decltype(T::variance)>> : std::true_type
{
};

// for "if constexpr" to work (avoid build errors if field does not exist)
// it must be inside a template:
template <class MSG_T>
void fromROS_variance(const MSG_T& msg, mrpt::obs::CObservationRange& obj)
{
if constexpr (has_variance<MSG_T>::value)
{
obj.sensedData.at(0).sensorNoiseStdDeviation = std::sqrt(msg.variance);
}
}
template <class MSG_T>
void toROS_variance(
MSG_T& msg, const mrpt::obs::CObservationRange::TMeasurement& m)
{
if constexpr (has_variance<MSG_T>::value)
{
msg.variance = mrpt::square(m.sensorNoiseStdDeviation);
}
}

bool mrpt::ros2bridge::fromROS(
const sensor_msgs::msg::Range& msg, mrpt::obs::CObservationRange& obj)
{
Expand All @@ -38,10 +58,7 @@ bool mrpt::ros2bridge::fromROS(
obj.sensedData.at(0).sensedDistance = msg.range;

// See: https://github.com/MRPT/mrpt/issues/1270
if constexpr (has_variance<sensor_msgs::msg::Range>::value)
{
obj.sensedData.at(0).sensorNoiseStdDeviation = std::sqrt(msg.variance);
}
fromROS_variance(msg, obj);

return true;
}
Expand All @@ -64,11 +81,7 @@ bool mrpt::ros2bridge::toROS(
msg[i].field_of_view = obj.sensorConeAperture;

// See: https://github.com/MRPT/mrpt/issues/1270
if constexpr (has_variance<sensor_msgs::msg::Range>::value)
{
msg[i].variance =
mrpt::square(obj.sensedData[i].sensorNoiseStdDeviation);
}
toROS_variance(msg[i], obj.sensedData[i]);
}

/// following part needs to be double checked, it looks incorrect
Expand Down

0 comments on commit 4b6e108

Please sign in to comment.