2008-03-01
GCC不能正确继承模板类
GCC编译器似乎不能理解继承自模板类里的数据成员。我还以为是代码的问题,郁闷了半天后,居然在Borland C++ 5.5下编译通过了。节省篇幅,我抽出主要部分。
B类的id本应该是从A类里继承来的,但用GCC编译会提示说变量id未定义。试着用Borland的编译器就很痛快地过了。我使用的是MinGW的GCC 3.4,但我在Unix-Center的机器上使用Unix和Linux版本的GCC 4.0编译仍然通不过,但Solaris上的Sun Studio的C++编译器则没问题。
难道就这么幸运地碰上GCC的Bug了?
template<class T>
class A{
protected:
T id;
};
template<class T>
class B: public A<T>{
public:
void setid(T i){
id=i;
}
void test1(){
cout<<"B::test1() ID: "<<id<<endl;
}
};
B类的id本应该是从A类里继承来的,但用GCC编译会提示说变量id未定义。试着用Borland的编译器就很痛快地过了。我使用的是MinGW的GCC 3.4,但我在Unix-Center的机器上使用Unix和Linux版本的GCC 4.0编译仍然通不过,但Solaris上的Sun Studio的C++编译器则没问题。
难道就这么幸运地碰上GCC的Bug了?
评论
DavidL
2008-03-12
这个是完整的答复:
http://blog.olivierlanglois.net/index.php/2007/08/19/dependent_names_and_two_phase_lookup
GFW封了,我贴一部分出来:
If VC++ does not complain, it is because it simply does not support the two phase lookup and compile templates only at instantiation point. GCC is a better compiler in that aspect because it complies with the C++ standard by implementing the Two-Phase lookup. You can easily fool VC++ with the following code:
template <class T>
class Test
{
public:
void Test() { foo(); }
private:
T m_a;
}
// Non dependent function declared after
// template declaration
int foo();
int main(int argc, char *argv[])
{
Test<int> testObj;
testObj.Test();
return 0;
}
GCC will generate an error when encountering the template complaining that foo() is not declared but VC++ will happily accept this code. Now that this is explained, note that except annoyance when porting code from VC++ to GCC or any other standard compliant compiler, the only consequence for MSVC of not being totally C++ standard compliant by not supporting the Two-Phase lookup is that if there are errors in a template, the compiler will delay their report at its instantiation instead of reporting them as soon as the template is encountered.
http://blog.olivierlanglois.net/index.php/2007/08/19/dependent_names_and_two_phase_lookup
GFW封了,我贴一部分出来:
If VC++ does not complain, it is because it simply does not support the two phase lookup and compile templates only at instantiation point. GCC is a better compiler in that aspect because it complies with the C++ standard by implementing the Two-Phase lookup. You can easily fool VC++ with the following code:
template <class T>
class Test
{
public:
void Test() { foo(); }
private:
T m_a;
}
// Non dependent function declared after
// template declaration
int foo();
int main(int argc, char *argv[])
{
Test<int> testObj;
testObj.Test();
return 0;
}
GCC will generate an error when encountering the template complaining that foo() is not declared but VC++ will happily accept this code. Now that this is explained, note that except annoyance when porting code from VC++ to GCC or any other standard compliant compiler, the only consequence for MSVC of not being totally C++ standard compliant by not supporting the Two-Phase lookup is that if there are errors in a template, the compiler will delay their report at its instantiation instead of reporting them as soon as the template is encountered.
DavidL
2008-03-12
需要 this.id=i;
发表评论
- 浏览: 4271 次
- 性别:

- 来自: 廊坊

- 详细资料
搜索本博客
最近加入圈子
链接
最新评论
-
彻底进入Linux了
Java IDE可以看看NetBeans、Eclipse什么的。类似于Ultra ...
-- by billgui -
彻底进入Linux了
ubuntu确实简单易用,但多媒体方面还是不如win
-- by lveyo -
彻底进入Linux了
root给了10g,对于你80g的硬盘也差不多了, 软件都装home下面的 编 ...
-- by spiritfrog -
在网页中插入数学公式的办 ...
试试看
-- by lix23 -
想不到这段代码居然是错的
孔乙己茴香豆茴字有四种写法
-- by ShiningRay






评论排行榜