BitPat here is a wrapper around a bitset.class BitPat{private:    boost::dynamic_bitset<> data;public:        bool BitPat::operator[](size_t coord) const    {        if(coord < length)            return data[coord];        else            throw std::out_of_range(std::string(Pattern coord out of range)                +  ( + std::to_string(coord%_width) + ,  + std::to_string(coord/_width) + )                 + [ + std::to_string(coord) + ]);    }    // vs:    bool BitPat::operator[](size_t coord) const    {        if(coord >= _length){            throw std::out_of_range(std::string(Pattern coord out of range)                +  ( + std::to_string(coord%_width) + ,  + std::to_string(coord/_width) + )                 + [ + std::to_string(coord) + ]);        }        return data[coord];    }};I have additional getters and setters where I'd implement bounds-checking like this. It generates a lot of boilerplate code though.As a first step maybe I should define some little formatting functions, but most importantly I'm curious which version of the if statements is more conventional.Not sure if omitting return statements like in the first case is good practice either.
Implementing bounds-checking with exceptions based on an existing bitset container
c++;error handling;boost;bitset
It doesn't really matter which direction you do the checking in - it's a matter of personal preference. Both are valid, semantically identical, and will almost certainly perform identically. One thing you can do to avoid having to rewrite all of this stuff is to have a single function that does bounds-checking:size_t boundsChecked(size_t coord) const {    // whichever order of this you prefer    if (coord >= _length) {        throw std::out_of_range(...);    }    return coord;}And then defer to that function everywhere you want to add bounds checking:bool operator[](size_t coord) const{        return data[boundsChecked(coord)];}