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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> 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 of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux