此代码无法在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;
}
C告诉我:
<source>:17:49: error: 'compute_type' is a private member of 'Outer<42>'
…当然是哪个,但是我试图从inside相同的类访问该成员。我不明白为什么不应该在那里访问它。我是否打过(并且应该归档)一个Clang错误?
您可以使用Godbolt's compiler explorer处的代码来戏弄。
这是core issue 1554。该标准尚不清楚如何对别名模板执行访问检查(在定义的上下文中或在使用的上下文中)。
当前方向是检查定义的上下文,这将使您的代码格式正确。