概念 TS 检查忽略私有访问修饰符

mag*_*001 10 c++ c++-concepts

我想写一个概念 Indexable 意思是一个序列要么有开始/结束,返回 RandomAccessIterator,要么 operator[] 被定义并返回一个非 void 类型的值。

我将Stroustrup 文章中的想法用于 Sequence 概念,并对其进行了扩充:

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

它适用于大多数情况,但在以下情况下失败:

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

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

static_assert(!Indexable<Bad>, "fail");
Run Code Online (Sandbox Code Playgroud)

出于某种原因,我的概念忽略了 operator[] 被定义为私有并返回 true 的事实。我错过了什么?

Cas*_*sey 1

这是 GCC bug #67225“带有约束结果的表达式约束关闭访问检查”,该问题将在 GCC10 中修复。