Simple class for 2D  1D array index conversion
Use modulus

Instead if this i-x*cols_ you can use the modulus operator i % cols_.

Use a unsigned type.

If you use unsigned there is no need to check for x &gt;= 0 &amp;&amp; y &gt;= 0. Also you can con throw the exception directly in check. And you just need check_xy.

    void IndexConv::check(unsigned x, unsigned y) const
    {
        if(x &gt;= rows_ || y &gt;= cols_) 
            throw std::out_of_range("IndexConv: parameter out of bounds.");
    }

    pair&lt;int, int&gt; IndexConv::to_xy(unsigned i) const
    {
        int x = i / cols_;
        int y = i % cols_;
        check(x, y);
        return make_pair(x, y);
    }

    int IndexConv::to_i(unsigned x, unsigned y) const
    {
        check(x, y);
        return x * cols_ + y;
    }


EDIT: Changed the answer to keep check_xy. Keeping check_i can map (0,2) to 2 when it should throw an exception (Following your example).