Using member variables inherited from the template base class (C ++)

I am trying to use member variables of a template base class in a derived class, as in this example:

template <class dtype> struct A { int x; }; template <class dtype> struct B : public A<dtype> { void test() { int id1 = this->x; // always works int id2 = A<dtype>::x; // always works int id3 = B::x; // always works int id4 = x; // fails in gcc & clang, works in icc and xlc } }; 

gcc and clang are both very picky about using this variable and require either explicit visibility or explicit use of "this". With some other compilers (xlc and icc), everything works as I expected. Is this a case of xlc and icc allowing code that is not standard, or a bug in gcc and clang?

+6
c ++ compiler-construction inheritance templates
source share
1 answer

You are probably compiling in lax mode in icc. In any case, since x is unqualified, it should not be looked for in any base classes that depend on template parameters. Therefore, in your code there is no place where x found, and your code is invalid.

Other names are looked up using a different search form (search for access to class members and qualified search). Both of these forms will study dependent base classes if they can (that is, if they are dependent, and thus they are looked up when creating a template instance when dtype known), all your other names depend on the template parameters)

Even GCC in its latest versions does not implement this correctly, and some dependent names are still resolved against dependent databases during unqualified searches.

+5
source share

All Articles