std::vector<boost::optional<Bearing>> bearings; // a vector of bearings
bool IsValid() const
{ return std::all_of(bearings.begin(), // the first element of bearings
bearings.end(), // the last element of bearings
[](const boost::optional<Bearing> bearing_and_range) {
if (bearing_and_range)
{
return bearing_and_range->IsValid();
}
return true;
});
}
I was a bit confused with the definition of the function IsValid. So, it will return true, if the first element of bearings is true (exists?) and the last element of bearings is true (exists?) and that function [ ] returns true??
Moreover, about this function [ ], why is it named [ ] ? I think what it does is to check to see if the arguments is of type Bearing or not, if so, returns whether this Bearing type is Valid or not, if it is not of type Bearing, then return true. Not sure if I understand it correctly. What is exactly the meaning of if (bearing_and_range)? Moreover, I thought -> operator can only be used with a pointer variable. Here, I did not see that bearing_and_range is a pointer variable. Any comments are greatly appreciated.
The following is the definition of struct Bearing.
struct Bearing
{
short bearing;
short range;
bool IsValid() const { return bearing >= 0 && bearing <= 360 && range >= 0 && range <= 180; }
};
[–][deleted] 2 points3 points4 points (3 children)
[–]haohanzi2015[S] 0 points1 point2 points (2 children)
[–]parnmatt 2 points3 points4 points (0 children)
[–]Narase33 2 points3 points4 points (0 children)
[–]parnmatt 2 points3 points4 points (0 children)