Gcc template overlay problem

template<class T>
class TBase
{
public:
 typedef int Int;

 struct TItem
 {
  T Data;
 };

 int value;
};

template<class T>
class TClass:public TBase<T>
{
public:
 TBase<T>::TItem item; // error here. only when using type defined in base class.

 void func()
 {
  TBase<T>::value ++; // no error here!
 }
};

int main(int argc, char *argv[])
{
 TClass<int> obj;
 return 0;
}

In the VC compiler and Borland C ++, both can compile it. But gcc cannot compile it, because it uses it twice to work with templates. VC or BCB do not care about an unknown template name. Is there any way to suppress this gcc function? Thank!

+3
source share
2 answers

Try:

typename TBase<T>::TItem item;

This link gives an explanation: http://pages.cs.wisc.edu/~driscoll/typename.html

+4
source

TItem is a type, so you need the typename keyword. Value is a field. The compiler resolves the value correctly, but it needs to be told that TItem is actually a type.

0
source

Source: https://habr.com/ru/post/1788612/


All Articles