Concept TS check ignores private access modifier

Viewed 189

I want to write a concept Indexable meaning that a sequence either has begin/end that return RandomAccessIterator, or operator[] is defined and returns a value of non void type.

I used ideas from Stroustrup's article for the Sequence concept and augmented it with:

template <class T>
concept bool Indexable = Sequence<T> || requires(T t, size_t n) {
    { t[n] } -> NotVoid;
};

It works on most cases but fails on the following:

struct Bad {
    std::vector<int> nums;

private:
    int& operator[](size_t ind) {
        return nums[ind];
    }
};

static_assert(!Indexable<Bad>, "fail");

For some reason my concept ignores the fact that operator[] is defined private and returns true. What am I missing?

1 Answers
Related