473,320 Members | 1,876 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,320 software developers and data experts.

Why do I need the typename keyword?

I'm trying to port code from VC.net to VC.net 2003. Here's a typical
piece of code that's often used:
template<class T> class PseudoContainer {
typedef T::iterator iterator; // this line will change in the next
example...
};
This compiles just fine under VC.net but it will not compile under VC.net
2003. In order to get this to compile with the new compiler, I need to add
the typename keyword, like so:
template<class T> class PseudoContainer {
typedef typename T::iterator iterator; // this line was changed from
the previous example...
};
Now, I can actually understand why you need the typename keyword. It's
because the compiler can't tell if T::iterator is a type or a static member
of T. Thus, you must tell it with the typename keyword. However, if that's
the case, then why did it compile just fine under VC.net?

Any insight into this will be greatly appreciated!
Jul 22 '05 #1
2 2060
"Anonymous" <ih*******@nowhere.com> wrote in message
news:gy*******************@monger.newsread.com...
I'm trying to port code from VC.net to VC.net 2003. Here's a typical
piece of code that's often used:
template<class T> class PseudoContainer {
typedef T::iterator iterator; // this line will change in the next
example...
};
This compiles just fine under VC.net but it will not compile under VC.net
2003. In order to get this to compile with the new compiler, I need to add
the typename keyword, like so:
template<class T> class PseudoContainer {
typedef typename T::iterator iterator; // this line was changed from
the previous example...
};
Now, I can actually understand why you need the typename keyword. It's
because the compiler can't tell if T::iterator is a type or a static member of T. Thus, you must tell it with the typename keyword. However, if that's
the case, then why did it compile just fine under VC.net?

Any insight into this will be greatly appreciated!

typename is used when writing templates and accessing other template member
*types*. Since you access the type inside of a template and not of an
instance, it would be hard work for the compiler to distinguish such things,
so you tell the compiler explicitly that it is a type defined inside a
template and not in a class or function (or template instance which is also
a class or function).

(E.g. vector<int> is a class, vector is a template).


Ioannis Vranos

Jul 22 '05 #2
"Anonymous" <ih*******@nowhere.com> wrote in message
news:gy*******************@monger.newsread.com
I'm trying to port code from VC.net to VC.net 2003. Here's a
typical piece of code that's often used:
template<class T> class PseudoContainer {
typedef T::iterator iterator; // this line will change in the
next example...
};
This compiles just fine under VC.net but it will not compile under
VC.net 2003. In order to get this to compile with the new compiler, I
need to add the typename keyword, like so:
template<class T> class PseudoContainer {
typedef typename T::iterator iterator; // this line was changed
from the previous example...
};
Now, I can actually understand why you need the typename keyword.
It's because the compiler can't tell if T::iterator is a type or a
static member of T. Thus, you must tell it with the typename keyword.
However, if that's the case, then why did it compile just fine under
VC.net?

Any insight into this will be greatly appreciated!

VC++ 7.1 is more standard conforming than VC++ 7.0, which means that some
things 7.0 allowed are no longer allowed in 7.1. Some of these changes may
have been just for the sake of being conformant to the standard; others may
be because 7.0 allowed you to do things that caused ambiguities (perhaps
only in special cases) that the compiler writers had not fully appreciated
at the time 7.0 was written.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #3

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

Similar topics

1
by: Xavier Decoret | last post by:
I got the following piece of code: template <class T,class V> class ClusterLeaf : public Cluster<T,V> { public: // ... protected: friend class Cluster<T,V>; friend class...
10
by: Alexander Malkis | last post by:
What's the semantical/syntactical difference between two keywords "class" and "typename" (apart from different spelling)? -- Best regards, Alex. PS. To email me, remove "loeschedies" from...
4
by: pervinder | last post by:
Hi, I get an error on line #3 while compiling the program segment on aix (xlc - va6) While this warning is not seen with gcc "test.cpp" line 200.18: 1540-0152 (W) A template dependent name that...
2
by: Levent | last post by:
Please consider the following Parent and Child template classes: template <class T> class Parent { public: typedef T type; typedef T& ref; /* pack of typedefs */ };
13
by: Staffan Langin | last post by:
Hello, In the following code-snippet, template<class Base> class Foo : public Base {
9
by: Kobe | last post by:
Is there any difference in: template <class T> vs. template <typename T> ?
6
by: 466 | last post by:
Hi all, I have this member in a template class: bool opsInsert(int nId, T pObj){ pairRet = insert(typename std::map<int,T>::value_type(nId, pObj)); } which does compile with gcc but not with...
7
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)...
5
by: Jess | last post by:
Hello, I've seen two forms of template declarations template<typename T> class A{...}; and template<class T>
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

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.