Topic: Q: template dependent name lookup


Author: Stephen.Clamage@Eng.Sun.COM (Steve Clamage)
Date: 1999/03/05
Raw View
Norbert Berzen <norbert@dune.gia.rwth-aachen.de> writes:

>>... base classes are not searched during name lookup ...
>does that mean `base classes' in general, or only
>template base classes which depend on some template
>parameter?

Template base classes that depend on some template parameter.

The reason for that lookup rule is that until the template
is instantiated, you don't know where to find the base-class
definition -- it might be a specialization unrelated to the
template definition you can see.

>Is the change in the name lookup rules the reason
>for introducing the keyword `typename'?

No. The purpose of typename was to allow compilers to be
able to parse a template definition and emit error messages
about constructs that were unconditionally incorrect.

Without the typename rule, the compiler cannot tell which names
are types, and cannot parse template definitions at all.
Consequently, no errors can be detected until the template
is instantiated, and then you get the same stupid error
repeated for every instantiation.

The typename rule was added at the request of C++ users who
wanted better error messages from the compiler.

>Finally here is one more question (sorry if I bother you):
>Which of the following `i1' .. `i8' member declarations
>inside of `struct X' are o.k.?

>   template<class T> struct B {
>     typedef int INT;
>   };

>   template<class T> struct C {
>     typedef int INT;
>   };

>   template<class T> struct X : public B<T> {
>     B<char>::INT i1;             // <---- o.k.?
OK, B<char> is a type, not a template
>     typename B<char>::INT i2;    // <---- o.k.?
>     B<T>::INT i3;                // <---- o.k.?
Not OK, B<T> is a dependent name, INT assumed not to be a type name.
>     typename B<T>::INT i4;       // <---- o.k.?
OK.
>     C<char>::INT i5;             // <---- o.k.?
OK, C<char> is a type, not a template
>     typename C<char>::INT i6;    // <---- o.k.?
>     C<T>::INT i7;                // <---- o.k.?
Not OK, C<T> is a dependent name, INT assumed not to be a type name.
>     typename C<T>::INT i8;       // <---- o.k.?
OK
>   };
--
Steve Clamage, stephen.clamage@sun.com
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: stephen.clamage@sun.com (Steve Clamage)
Date: 1999/02/27
Raw View
Norbert Berzen <norbert@dune.gia.rwth-aachen.de> writes:

>to make it short: what output should the following progam generate?
>`egcs-1.1.1' and `tcc-4.1.2' both generate: 4.
>I think the program should generate: 8.
>Do I misunderstand clause [14.6.2(4)] ?

>// assume sizeof (int) == 4
>// assume sizeof (double) == 8

>#include <iostream.h>

>typedef double A;

>template<class T> struct B {
>  typedef int A;
>};

>template<class T> struct X : public B<T> {
>  A a;
>};

>int
>main ()
>{
>  X<char> xc;
>  cout << sizeof (xc.a) << endl;
>}

The name lookup rules for templates have changed over time.
According to the standard, base classes are not searched during
name lookup, but that is a recent language change.

It appears the two compilers you used do not fully conform to
the standard.  In particular, if tcc-4.1.2 means Turbo C++ 4.1.2,
it predates this change in the language by several years!

--
Steve Clamage, stephen.clamage@sun.com


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Norbert Berzen <norbert@dune.gia.rwth-aachen.de>
Date: 1999/03/01
Raw View

Thanks for your advice,

>if tcc-4.1.2 means Turbo C++ 4.1.2, ...
No, TenDRA-4.1.2 was meant.

>... base classes are not searched during name lookup ...
does that mean `base classes' in general, or only
template base classes which depend on some template
parameter?

Is the change in the name lookup rules the reason
for introducing the keyword `typename'?

Finally here is one more question (sorry if I bother you):
Which of the following `i1' .. `i8' member declarations
inside of `struct X' are o.k.?

   template<class T> struct B {
     typedef int INT;
   };

   template<class T> struct C {
     typedef int INT;
   };

   template<class T> struct X : public B<T> {
     B<char>::INT i1;             // <---- o.k.?
     typename B<char>::INT i2;    // <---- o.k.?
     B<T>::INT i3;                // <---- o.k.?
     typename B<T>::INT i4;       // <---- o.k.?
     C<char>::INT i5;             // <---- o.k.?
     typename C<char>::INT i6;    // <---- o.k.?
     C<T>::INT i7;                // <---- o.k.?
     typename C<T>::INT i8;       // <---- o.k.?
   };


Thanks in advance,
--
Norbert (norbert@dune.gia.rwth-aachen.de  [134.130.159.38])
N. Berzen, Geod. Inst. der RWTH-Aachen, Templergraben 55, 52056 Aachen


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Norbert Berzen <norbert@dune.gia.rwth-aachen.de>
Date: 1999/02/25
Raw View

Hi all,

to make it short: what output should the following
progam generate?
`egcs-1.1.1' and `tcc-4.1.2' both generate: 4.
I think the program should generate: 8.
Do I misunderstand clause [14.6.2(4)] ?

--<cut here>-- --<cut here>-- --<cut here>--

// assume sizeof (int) == 4
// assume sizeof (double) == 8

#include <iostream.h>

typedef double A;

template<class T> struct B {
  typedef int A;
};

template<class T> struct X : public B<T> {
  A a;
};

int
main ()
{
  X<char> xc;
  cout << sizeof (xc.a) << endl;
}


--
Norbert (norbert@dune.gia.rwth-aachen.de  [134.130.159.38])
N. Berzen, Geod. Inst. der RWTH-Aachen, Templergraben 55, 52056 Aachen


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]