I’m using a boost::multi_array and when I need to check if a given coordinate is within bounds, I do this:
bool MapData::IsWithinBounds(TileArray3D::index x, TileArray3D::index y, TileArray3D::index z) const {
return (x >= 0 and x < GetWidth()) and
(y >= 0 and y < GetHeight()) and
(z >= 0 and z < GetDepth());
}
where TileArray3D is defined as:
typedef boost::multi_array<TileID, 3> TileArray3D;
and TileID is:
BOOST_STRONG_TYPEDEF(int, TileID);
The signature of GetWidth/Height/depth is:
TileArray3D::size_type GetWidth() const;
But TileArray3D::size_type is unsigned and TileArray3D::index is signed. Am I using these types in the wrong way? Or is there a way around this? Should I just cast the index‘s to size_type‘s? or can problems occur?
Thanks in advance, ell.
If the size of
size_typeis at least as large as the size ofindex, just cast the indices tosize_typefor that comparison. Since you checked for non-negativity before, there can’t be overflow then, so it’s safe.