GCC问题:使用依赖于模板参数的基类成员 [英] GCC issue: using a member of a base class that depends on a template argument

查看:90
本文介绍了GCC问题:使用依赖于模板参数的基类成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码无法使用gcc进行编译,但可以使用Visual Studio进行编译:

The following code doesn't compile with gcc, but does with Visual Studio:

template <typename T> class A {
public:
    T foo;
};

template <typename T> class B: public A <T> {
public:
    void bar() { cout << foo << endl; }
};

我得到了错误:

test.cpp:在成员函数"void B :: bar()"中:

test.cpp: In member function ‘void B::bar()’:

test.cpp:11:错误:在此范围内未声明"foo"

test.cpp:11: error: ‘foo’ was not declared in this scope

但是应该!如果我将bar更改为

But it should be! If I change bar to

void bar() { cout << this->foo << endl; }

然后进行 编译,但是我认为我不必这样做. GCC遵循的C ++正式规范中是否有某些内容,还是只是一个古怪的东西?

then it does compile, but I don't think I have to do this. Is there something in the official specs of C++ that GCC is following here, or is it just a quirk?

推荐答案

此问题在 gcc-3.4 . C ++解析器在该版本中的要求更加严格-按照规范,但是对于具有旧版或多平台代码库的人来说仍然有些烦人.

This changed in gcc-3.4. The C++ parser got much more strict in that release -- per the spec but still kinda annoying for people with legacy or multi-platform code bases.

这篇关于GCC问题:使用依赖于模板参数的基类成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆