[LC++]Why new behaviour of g++?
Dr Mark H Phillips
mark at austrics.com.au
Wed Feb 16 16:18:02 UTC 2005
Hi,
The recent versions of g++ (in contrast to 3.3.* versions) will no
longer compile:
#include <iostream>
template<typename T> struct SixStore {
int store; SixStore(): store(6) {}
};
template<typename T> struct Six: public SixStore<T> {
int six() {return store;}
};
int main() {
Six<char> s;
std::cout<<"six = "<<s.six()<<std::endl;
}
It complains with:
tst3.cc: In member function `int Six<T>::six()':
tst3.cc:8: error: `store' undeclared (first use this function)
If I remove the template stuff however, it compiles fine. I had
a look on the GCC website and on the page
http://gcc.gnu.org/bugs.html#known
I found the following information:
This also affects members of base classes, see [14.6.2]:
template <typename> struct A
{
int i, j;
};
template <typename T> struct B : A<T>
{
int foo1() { return i; } // error
int foo2() { return this->i; } // OK
int foo3() { return B<T>::i; } // OK
int foo4() { return A<T>::i; } // OK
using A<T>::j;
int foo5() { return j; } // OK
};
and foo1() seems to be pretty much what I was unsuccessfully trying to
do! But why does g++ now outlaw this? Presumably the answer lies in
14.6.2 of the standard, but I do not have a copy of this and I can't
find it anywhere on the web. Can anyone enlighten me on what is happening
here and why this kind of access to base-class members has been outlawed??
Thanks,
Mark.
More information about the tuxCPProgramming
mailing list