【发布时间】:2019-08-02 00:11:27
【问题描述】:
下面的编译没有错误:
template<int j, int i>
struct TemplateClass {
int arr[i];
};
struct A {
inline static constexpr int n = 123;
};
template<int j> struct B {
void func() {
A a;
TemplateClass<j, a.n> c;
}
};
int main() {
B<456> b;
b.func();
}
但是,使用 GCC 编译,如果我们将变量 A a在函数func中,像这样:
template<int j> struct B {
A a;
void func() {
TemplateClass<j, a.n> c;
}
};
使用 MSVC 编译不会出错。 Compare the two compilers,
- 我不明白为什么会出现错误。这是一个错误吗?
- 是否有规避此错误的解决方法?
【问题讨论】:
-
IIRC 这是上周被问到的。我会试着找到它。我相信这是缺陷报告的主题。
-
是只使用
A::n的解决方法吗? -
@FantasticMrFox,是的,这行得通!
decltype(a)::n也可以。
标签: c++ templates compiler-errors compiler-bug