Class 无法访问它自己的私有静态 constexpr 方法 - Clang 错误?

Class can't access its own private static constexpr method - Clang bug?

此代码无法在 Clang (6,7,8,9,trunk) 中编译,但在 GCC (7.1, 8.1, 9.1) 中编译得很好:

template<class T> struct TypeHolder { using type = T; };

template<int i>
class Outer {
private:
    template<class T> 
    static constexpr auto compute_type() {
        if constexpr (i == 42) {
            return TypeHolder<bool>{};
        } else {
            return TypeHolder<T>{};
        }
    }

public:
    template<class T>
    using TheType = typename decltype(Outer<i>::compute_type<T>())::type;
};

int main() {
    Outer<42>::TheType<int> i;
}

Clang 告诉我:

<source>:17:49: error: 'compute_type' is a private member of 'Outer<42>'

… 当然是这样,但我正试图从 内部 访问该成员 class。我不明白为什么在那里不能访问它。我遇到(并且应该提交)Clang 错误了吗?

您可以尝试使用 Godbolt's compiler explorer 中的代码。

这是core issue 1554。该标准不清楚如何对别名模板执行访问检查(在定义的上下文中,或在使用的上下文中)。

当前的方向是检查定义的上下文,这将使您的代码格式正确。