gcc 3.4.5: Partially specialized descendant of template doesn't see members of base

Mazay Mazzzy2005@yandex.ru
Sat Oct 7 09:08:00 GMT 2006


Hello.

I'm not sure, if this code should to compile in accordance with ISO, but it
compiles by g++ 4.1.1 and by VC2003-2005.

Here is a little test for it:

---------------------------------------------------------
template<class T>
class Base
{
protected:
    T x;
    Base() {}
};

template <class T,int num>
class Der: public Base<T>
{
public:
    using Base<T>::x; 

    Der():Base<T>() {}
    int get();
};

template <class T>
class Der<T,4>: public Base<T>
{
public:
    using Base<T>::x; //!!

    Der():Base<T>() {}
    int get();
};
    
template <class T,int num>
int Der<T,num>::get()
{
    return x-num;
} 

template <class T>
int Der<T,4>::get()
{
    return x+4; //38 row. The error is here. x is not declared in this scope.
}  

int main()
{
    return 0;
}
---------------------------------------------------------

Compilation output:
---------------------------------------------------------
C:\prg\test\mingw_templ>g++ main.cpp
main.cpp: In member function `int Der<T, 4>::get()':
main.cpp:38: error: `x' was not declared in this scope
---------------------------------------------------------

Compiler version information:
---------------------------------------------------------
C:\prg\test\mingw_templ>g++ -v
Reading specs from C:/Install/MinGW/bin/../lib/gcc/mingw32/3.4.5/specs
Configured with: ../gcc-3.4.5/configure --with-gcc --with-gnu-ld --with-gnu-as --host=mingw32 --target=mingw32 --prefix=
/mingw --enable-threads --disable-nls --enable-languages=c,c++,f77,ada,objc,java --disable-win32-registry --disable-shar
ed --enable-sjlj-exceptions --enable-libgcj --disable-java-awt --without-x --enable-java-gc=boehm --disable-libgcj-debug
 --enable-interpreter --enable-hash-synchronization --enable-libstdcxx-debug
Thread model: win32
gcc version 3.4.5 (mingw special)
---------------------------------------------------------

An error can be fixed by replacing of string "return x+4;"
 by "return this->x+4;". But it is fairly bad decision.
I suppose that this is a bug (because it compiles by 4.1.1), but I
can't use newer compiler, because 3.4.5 is the lattest in MinGW. And I
have to use MinGW :( .
I hope, that some better workaround or patch for compiler exists, and
somebody here can help me.
        

Regards,
Mazay.




More information about the Gcc-help mailing list