473,508 Members | 2,140 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Q: typename or not typename?

Hi,

I recently switched compilers (VC++7.1 to VC++8) and it started
complaining about my use of 'typename' in template classes. So far, I
have never fully understood where I need to put 'typename' and where
not. Please consider the following snippet:

#include <list>

template <typename T>
struct test_case
{
test_case (/* (1) */T::iterator i = (/* (2) */T::iterator ())
{
(/* (3) */T::iterator ii;
}
};

int main (int argc, char* argv [])
{
test_case <std::list <int> > foo;
}

As you can see, I marked three spots. In (1), the compiler requires
me to put 'typename'. In (2) I am not allowed to. In (3) it compiles
with or without.

Why those differences? And what is the reasoning behind requiring it
at all?

Thanks for your time!
--
jb

(reply address in rot13, unscramble first)
Mar 14 '06 #1
2 1859
Jakob Bieling wrote:
I recently switched compilers (VC++7.1 to VC++8) and it started
complaining about my use of 'typename' in template classes. So far, I
have never fully understood where I need to put 'typename' and where
not. Please consider the following snippet:

#include <list>

template <typename T>
struct test_case
{
test_case (/* (1) */T::iterator i = (/* (2) */T::iterator ())
{
(/* (3) */T::iterator ii;
(1): yes, (2): no, (3): yes.
}
};

int main (int argc, char* argv [])
{
test_case <std::list <int> > foo;
}

As you can see, I marked three spots. In (1), the compiler requires
me to put 'typename'. In (2) I am not allowed to. In (3) it compiles
with or without.

Why those differences? And what is the reasoning behind requiring it
at all?


It's required for (1) because you need to help your compiler to understand
that what it's compiling is a declaration (same with (3)). However, since
(2) is the expression, 'typename' has to be omitted.

Imagine that you try to instantiate your 'test_case' template with a class
that has 'iterator' as a function or a data member? _You_ know that it is
not in 'std::list<int>' but when the compiler first looks at the template,
it has no way to know.

V
--
Please remove capital As from my address when replying by mail
Mar 14 '06 #2
Jakob Bieling wrote:
Hi,

I recently switched compilers (VC++7.1 to VC++8) and it started
complaining about my use of 'typename' in template classes. So far, I
have never fully understood where I need to put 'typename' and where
not.
You need it if the type depends on a template argument.
Please consider the following snippet:

#include <list>

template <typename T>
struct test_case
{
test_case (/* (1) */T::iterator i = (/* (2) */T::iterator ())
{
(/* (3) */T::iterator ii;
}
};

int main (int argc, char* argv [])
{
test_case <std::list <int> > foo;
}

As you can see, I marked three spots. In (1), the compiler requires
me to put 'typename'. In (2) I am not allowed to. In (3) it compiles
with or without.
Hmm, I would have expected that it's needed in all three places. My compiler
(g++) requires it in (1) and (3) and doesn't care in (2).
Why those differences? And what is the reasoning behind requiring it
at all?


Because in some situations, it's quite some extra work for the compiler to
find out whether T::iterator is the name of a type or a variable, since it
doesn't know anything about T yet when parsing the template. To reduce
context dependancies, 'typename' is used to tell the compiler that the name
denotes a type.

Mar 14 '06 #3

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

Similar topics

1
3834
by: I wish | last post by:
I wrote some test code template <typename T> class A {}; class B{}; template < template <typename T> typename U > class C {}; int main( void )
2
2677
by: Chris Foster | last post by:
Hi, I'm having some difficulty using types which are defined in a base class inside a derived class. The problem crops up using template classes. The following test code encapsulates what I'd...
1
1748
by: lutorm | last post by:
Hi all, I'm working on migrating my code from KCC to gcc, and I'm having some issues with "implicit typename" warnings from gcc. Essentially, what happens is described by this example: ...
13
1691
by: Staffan Langin | last post by:
Hello, In the following code-snippet, template<class Base> class Foo : public Base {
7
2111
by: StephQ | last post by:
First of all: distinction of keywords typename and class in template arguments. Accoarding to a post in a well known moderated group: "There are three possibilities for template arguments: 1)...
0
7228
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
7128
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
7332
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7393
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...
0
7502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5635
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
1565
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
769
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
426
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.