GCC模板问题

时间:2010-06-14 21:07:24

标签: c++ templates gcc

  

可能重复:
  problem with template inheritance

此代码无法在GCC中编译:

template <typename T>
struct Base
{
public:
 int x;
};

template <typename B>
struct Middle : public B
{
};

template <typename T>
struct Sub : public Middle<Base<T> >
{
public:
 void F()
 {
  x=1; // error: ‘x’ was not declared in this scope
 }
};

如果BaseSub不是模板类,则不会抱怨。 VC处理它。

为什么?

1 个答案:

答案 0 :(得分:4)

使用this->x = 1;告诉编译器x是(模板)相关名称。 注意:GCC根据标准做得不错,MSVC只是更宽容。