GCC issue: using a member of a base class that depends on a template argument(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 问题:使用依赖于模板参数的基类成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:GCC 问题:使用依赖于模板参数的基类成员


基础教程推荐
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- CString 到 char* 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01