c++ - 类无法访问其自己的私有(private)静态 constexpr 方法 - Clang 错误?

标签 c++ language-lawyer c++17 clang++

这段代码不能在 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>'

......当然是这样,但我正试图从同一个类中访问该成员。我不明白为什么它不应该在那里访问。我是否遇到了(并且应该提交)Clang 错误?

您可以在 Godbolt's compiler explorer 上玩弄代码.

最佳答案

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

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

关于c++ - 类无法访问其自己的私有(private)静态 constexpr 方法 - Clang 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59883060/

相关文章:

c++ - 普通旧数据 - 对齐要求

c++ - Visual Studio 2017 中关于 constexpr 表达式的编译问题

c++ - 管理琐碎类型

c++ - 静态库的全局变量的静态初始化和销毁​​不会在 g++ 中发生

C++ - 将 long 转换为 float 或 double 四舍五入值

c++ - 将 for 循环从 matlab 转移到 c++

c++ - 在//C++ 注释中使用\\是否合法? (C++ 注释中的 LaTeX 方程)

c++ - 有没有一种方法可以在不专门化强制转换的情况下转换存储在 QVariant 中的基类型?

c++ - 将模板类型参数作为模板非类型参数的类型传递

c++ - 如果 constexpr 与 sfinae