这是一个涉及从模板类继承的测试片段:
#include <iostream>
using namespace std;
template <typename T>
struct A {
T x;
};
template <typename T>
struct B : public A<T> {
B(T x, T y) : A<T>{ x }, y{ y } { }
T y;
};
template <typename T>
struct C : public B<T> {
C(T x, T y, T z) : B<T>{ x, y }, z{ z } { }
T z;
// when I typed `this->`, Intellisense didn't give me the option `x`,
// which is iherited from `A<T>` and should appear in the autocomplete pop-up.
// In contrast, `this->` did give the option `y`.
void test() { cout << this->x << ", " << this->y << ", " << z << endl; }
};
int main() {
C<int> c{ 1, 2, 3 };
c.test(); // prints out `1, 2, 3` as expected
return 0;
}
代码编译并运行。但是void C<T>::test()
,当我尝试使用指针访问x
在间接基类中定义的成员时,没有出现在 Intellisense 自动完成弹出窗口中。另一方面,直接继承的成员确实出现了。A<T>
this
x
y
我用 VS2019 和 VSCode+gcc 进行了测试,两者都给出了相似的结果。
但我知道 Intellisense 是一个非常强大的引擎,所以我想知道我是否正确地从模板类继承?如果我错了,正确的方法是什么?
我身边也有同样的情况。而且我认为这是C++ Intellisense而不是C++ 语法标准的真正问题。
事实上,该项目构建运行良好,可以打印出 的值,x
没有任何错误,证明问题与C++语法标准无关。
它只是一个纯 C++ IntelliSense 问题。并且 Intellisense 无法从二级结构中获取成员。
我已经在我们的 DC 论坛上报告了这个问题。如果我没有详细描述问题,您可以投票并添加任何评论,以便它得到更多微软的关注。我希望团队会给你一个满意的答复。
建议
由于该过程可能需要很长时间,作为建议,到目前为止,您还可以让 struct C
inherit A
。到目前为止,Intellisense无法从A
(二级结构)中获取成员。
template <typename T>
struct C : public B<T>, public A<T> {
C(T x, T y, T z) : B<T>{ x, y }, z{ z } { }
T z;
// when I typed `this->`, Intellisense didn't give me the option `x`,
// which is iherited from `A<T>` and should appear in the autocomplete pop-up.
// In contrast, `this->` did give the option `y`.
void test() { cout <<this->x << ", " << this->y << ", " << z << endl; }
};
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句