Policy-based design with an empty checking policy

SmartArray is parameterized by a CheckingPolicy. NoChecking provides no check() member at all (it's meant to be optimized away via a policy that never calls check), while BoundsChecking does. What happens when SmartArray<int, NoChecking> is instantiated and operator[] is actually called?

main.cpp
struct NoChecking {
    // intentionally empty: no check() member
};

struct BoundsChecking {
    static void check(size_t idx, size_t size) {
        if (idx >= size) throw std::out_of_range("bad index");
    }
};

template <typename T, typename CheckingPolicy>
class SmartArray {
public:
    SmartArray(size_t n) : data_(n) {}
    T& operator[](size_t idx) {
        CheckingPolicy::check(idx, data_.size());   // always called unconditionally
        return data_[idx];
    }
private:
    std::vector<T> data_;
};

int main() {
    SmartArray<int, NoChecking> arr(10);
    arr[3] = 42;
}

9 of 44 in Design Patterns