Question

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.

Was it helpful?

Solution

If the size of size_type is at least as large as the size of index, just cast the indices to size_type for that comparison. Since you checked for non-negativity before, there can't be overflow then, so it's safe.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top