473,387 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

template and disambiguation

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();
}
Jul 22 '05 #1
2 7914
Xenos wrote:

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.

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();
}


To paraphrase it again:
You have to use the template construct when you have things like the following:

dependent_name::template nested_template_id
dependent_name.template nested_template_id
dependent_name->template nested_template_id

where
dependent_name is the name of a class template that depends on a
template parameter. In the snippet above, in template_func(void),
A<T> and a are dependent names.

nested_template_id is a function template or class template name,
B<int>, B<T1>, f<int>(), f<T1>().

Why you need it? If you don't give it, the compiler will treat the "<"
in nested_template_id as a less_than operator. It will not attempt to find
out that B or f are actually templates; the compiler's default behaviour is
to assume they are not. AFAIK, the reason for that is that B or f can be
declared and defined in any particular specialization of the class corresponding
to the dependent_name, not necessarily in the "original" template definition.
What is more, a specialization can redefine them altogether, e.g. make them
non-templates.

The key point here is the "dependent_name". A compiler, generally,
cannot assume it knows the nested definitions of a dependent name.

On the other hand, in the last example above (non_template_func(void)), A<K> and
a are not dependent names. The compiler now deals with a specialization
and, therefore, it knows that B and bar are template ids. Thus, conceptually,
there is no need to give it a "template" hint in this case (the current standard
would not allow it anyway (again AFAIK), but there are opinions that it should,
for symmetry).

Denis
Jul 22 '05 #2
"Xenos" <do**********@spamhate.com> wrote in message news:<c6*********@cui1.lmms.lmco.com>...
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:


Without going into too much detail or quoting directly from the
standard, I think there are a few clarifications that might help you.

1) the template and typename keywords are only need inside
declarations and definitions of other templated types.

2) Typename is needed because the compiler cannot know when a type
parameter of a template is used as a identifier (e.g. T::A ), whether
it is referring to a type name, or being used for scope resolution.
Thus the typename keyword is required in the case when T::A refers to
a type; in all other cases it is assumed to be for scope-resolution.

3) The template keyword is needed for more complicated declarations
when a template parameter
a) is itself a template, e.g. consider the following declarations:
template <class T, class U> class A {
U<T> u; // error: U is not a template type

template <class T, template <class> U > class B {
U<T> u; // this is now ok
};
the second version explicitly informs the compiler that class U
is allowed to be used as a template type within template class B.

b) has a templated member (function or class) ... the examples
from the GCC page pertain to this case.

In both cases, the compiler would otherwise have no way of knowing
that the attempt to use a template parameter (or member of a template
parameter) as a template was valid.

Hopefully you can now understand the GCC examples you posted a little
better.

Dave Moore
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
by: nikola | last post by:
Hi all, I was working with a simple function template to find the min of two values. But since I would like the two values to be different (type) I dont know what kind of value (type) it will...
1
by: John Collins | last post by:
I just tried to build under GNU C++ 3.4.1 and then 3.4.3 a program which was working fine under G++ 3.3.* It uses template members "shm_cast" to obtain a pointer "foo *" to a structure in shared...
3
by: Rüdiger Knörig | last post by:
I've a nasty problem here (g++ 3.4/4.0): I've a template datatype mltreenode<T> from which I instanced a STL vector vector< mltreenode<T> * > without any problems. Calling methods on this object...
2
by: Rudy Ray Moore | last post by:
Whenever I get any error with Vc++7.1/.net/2003, it is followed by huge ammounts of "template assistance" error messaging referencing template code (MTL) that has nothing to do with the error. ...
10
by: Petr Jakeš | last post by:
I have a standard 12-key mobile phone keypad connected to my Linux machine as a I2C peripheral. I would like to write a code which allows the text entry to the computer using this keypad (something...
2
by: Imre | last post by:
Hi Please consider the following code: template <class T> struct Base; template <template <typenameclass T, typename P> struct Base<T<P {
8
by: IR | last post by:
Hello, Does anyone know why this declaration compiles: template< template<typenameclass T> class X { /*...*/ }; while this one doesn't: template< template<typenametypename T>
19
by: aaragon | last post by:
Hi everyone. A very simple question. I would like to know what is better in terms of performance. I want to use a simple function to obtain the minimum of two values. One way could be using a...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.