X
Xenos
The new version of GCC is out and in its list of changes, it talks about the
C++ Standard's requirements for using the typename and template keywords to
disambiguate dependent names. I'm use to seeing typename being used, but
I've never seen template used for this purpose. I've studied there example
for a long time, and still don't understand what situations call for the
template keyword (and reading the standard, to me, is Greek). Can some
explain this. The GCC example follows:
You must now use the typename and template keywords to disambiguate
dependent names, as required by the C++ standard.
struct K {
typedef int mytype_t;
};
template <class T1> struct A {
template <class T2> struct B {
void callme(void);
};
template <int N> void bar(void)
{
// Use 'typename' to tell the parser that T1::mytype_t names
// a type. This is needed because the name is dependent (in
// this case, on template parameter T1).
typename T1::mytype_t x;
x = 0;
}
};
template <class T> void template_func(void)
{
// Use 'template' to prefix member templates within
// dependent types (a has type A<T>, which depends on
// the template parameter T).
A<T> a;
a.template bar<0>();
// Use 'template' to tell the parser that B is a nested
// template class (dependent on template parameter T), and
// 'typename' because the whole A<T>::B<int> is
// the name of a type (again, dependent).
typename A<T>::template B<int> b;
b.callme();
}
void non_template_func(void)
{
// Outside of any template class or function, no names can be
// dependent, so the use of the keyword 'typename' and 'template'
// is not needed (and actually forbidden).
A<K> a;
a.bar<0>();
A<K>::B<float> b;
b.callme();
}
C++ Standard's requirements for using the typename and template keywords to
disambiguate dependent names. I'm use to seeing typename being used, but
I've never seen template used for this purpose. I've studied there example
for a long time, and still don't understand what situations call for the
template keyword (and reading the standard, to me, is Greek). Can some
explain this. The GCC example follows:
You must now use the typename and template keywords to disambiguate
dependent names, as required by the C++ standard.
struct K {
typedef int mytype_t;
};
template <class T1> struct A {
template <class T2> struct B {
void callme(void);
};
template <int N> void bar(void)
{
// Use 'typename' to tell the parser that T1::mytype_t names
// a type. This is needed because the name is dependent (in
// this case, on template parameter T1).
typename T1::mytype_t x;
x = 0;
}
};
template <class T> void template_func(void)
{
// Use 'template' to prefix member templates within
// dependent types (a has type A<T>, which depends on
// the template parameter T).
A<T> a;
a.template bar<0>();
// Use 'template' to tell the parser that B is a nested
// template class (dependent on template parameter T), and
// 'typename' because the whole A<T>::B<int> is
// the name of a type (again, dependent).
typename A<T>::template B<int> b;
b.callme();
}
void non_template_func(void)
{
// Outside of any template class or function, no names can be
// dependent, so the use of the keyword 'typename' and 'template'
// is not needed (and actually forbidden).
A<K> a;
a.bar<0>();
A<K>::B<float> b;
b.callme();
}