0

Here is a testing snippet involving inheriting from template classes:

#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;
}

The code compiles and runs. But in void C<T>::test(), when I tried to access the member x defined in the indirect base class A<T> using this pointer, x didn't show up in the Intellisense autocomplete pop-up. On the other hand, the directly inherited member y did show-up.

I tested with VS2019 and VSCode+gcc, both of which gave similar results.

But I know that Intellisense is a very powerful engine, so I wonder if I'm doing inheritance from template classes correctly or not? If I'm wrong, what is the proper way?

Leon Cruz
  • 345
  • 3
  • 11
  • 3
    Intellisense is not as accurate as the compiler proper. It can (and does) make mistakes. – Paul Sanders Nov 10 '20 at 07:43
  • why use `this->` to get to `x` – rioV8 Nov 10 '20 at 10:33
  • @rioV8 x is not a dependent name in the context of a class template, see [Why do I have to access template base class members through the this pointer?](https://stackoverflow.com/questions/4643074/why-do-i-have-to-access-template-base-class-members-through-the-this-pointer) – Leon Cruz Nov 10 '20 at 11:33
  • @LeonCruz You can still learn a new C++ thing every day – rioV8 Nov 10 '20 at 15:03

1 Answers1

1

I also get the same situation in my side. And I think it is a real issue of C++ Intellisense rather than C++ syntax standard.

In fact, the project builds and runs well and can print the value of x without any errors, which proves that the issue is not related to C++ syntax standard.

And it is just a pure C++ IntelliSense problem. And Intellisense cannot get the member from the second-level struct.

I have reported the issue on our DC Forum. And you can vote it and add any comments if I did not describe the issue in detail so that it will get more Microsoft's attention. And I hope the Team will give you a satisfactory reply.

Suggestion

Since the process might take a long time, as a suggestion, so far, you could also let struct C inherit A. And Intellisense cannot get the member from the A(a second-level struct) so far.

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; }
};
Mr Qian
  • 21,064
  • 1
  • 31
  • 41
  • Thanks for the suggestion. But I think adding A as another base just to satisfy Intellisense may not be a good idea and may cause potential problems if it's not a simple demo case but a more realistic scenario. Hope this can be fixed soon. – Leon Cruz Nov 10 '20 at 10:15