This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Strange template behaviour with gcc 3.4 (20030813 and 20031001)


> 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!!


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]