Re: typedef in template class ---GCC3.4.5 vs MSVC8
zade <zhaohongchao@gmail.com> writes:
template<class T>
struct A
{
typedef T t_type;
};
template<class T>
struct B:public A<T>
{
void foo(t_type i)
t_type is unqualified, so it is looked up when the template is
parsed. The members of the A base template aren't considered, because
there is no telling whether the base template will actually generate
the base class.
Imagine the specializations
template<>
struct A<int>
{
unsigned int t_type;
};
template<>
struct A<char>
{
// no member named t_type
};
which declare t_type to something inappropriate or don't declare it at
all.
{}
};
int main()
{
B<int> b;
b.foo(0);
return 0;
}
This code can be compiled in MSVC8, but not in GCC3.4.5, and the error
message is :"t_type has not been declared", But it is declared in
A<T>,which is publicly inherited by B<T>.
So which one is right?
gcc.
If you defer t_type's lookup to instantiation time, the t_type member
of the A base template will be considered (provided there is no
specialization). You can achieve this by making t_type dependant (on
the teplate parameter). E.g.:
template<class T>
struct B:public A<T>
{
typedef typename A<T>::t_type t_type;
void foo(t_type i)
{
}
};
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]