以下编译没有错误:
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 编译时,如果我们在函数中创建变量的成员变量,则会出现错误“在常量表达式中使用 'this'”,如下所示:A afunc
template<int j> struct B {
A a;
void func() {
TemplateClass<j, a.n> c;
}
};
使用 MSVC 编译不会出错。比较两个编译器,
- 我不明白为什么这会出错。这是一个错误吗?
- 是否有解决此错误的解决方法?