Strange template behaviour with gcc 3.4 (20030813 and 20031001)
Oliver Kullmann
O.Kullmann@Swansea.ac.uk
Mon Oct 6 10:54:00 GMT 2003
> Hello,
>
> I'd like to know if this is a bug in the experimental gcc I'm using or if I'm
> doing something wrong:
>
> template<class A, class B>
> struct pair_t{
> A first;
> B second;
> };
>
> template<class A, class B>
> class something_t : public pair_t<A, B>{
> public:
> void strange(A i){
> first = i;
> }
> };
>
> int main(){
> return 0;
> }
>
Hi,
the above code is incorrect, since non-dependent names like "first" are bound
at the point of *definition* of the template (*not* its instantiation), and
they are not looked up in dependent base classes (like "pair_t<A,B>", which depends on
the template parameters A and B),
This issue is discussed in depth in
@Book{JV2002,
author = {David Vandevoorde and Nicolai M. Josuttis},
title = {C++ Templates: The Complete Guide},
publisher = {Addison-Wesley},
year = 2002,
address = {Boston},
month = {November},
note = {ISBN 0-201-73484-2; QA76.73.C153 V37 2003},
annote = {Vorhanden.}
}
The solution is to make the name "first" dependent. Two solutions:
1) Use this -> first
2) Use pair_t<A,B>::first
instead of "first".
According to JV2000 the first solution in general is preferable.
Oliver
P.S. I'm happy to hear that now g++ is handling this issue correctly!!
More information about the Gcc-help
mailing list