Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Why do I need the typename keyword?

Reply
Thread Tools

Why do I need the typename keyword?

 
 
Anonymous
Guest
Posts: n/a
 
      04-13-2004
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!


 
Reply With Quote
 
 
 
 
Ioannis Vranos
Guest
Posts: n/a
 
      04-13-2004
"Anonymous" <(E-Mail Removed)> wrote in message
news:gyYec.1179$(E-Mail Removed)...
> 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

 
Reply With Quote
 
 
 
 
John Carson
Guest
Posts: n/a
 
      04-13-2004
"Anonymous" <(E-Mail Removed)> wrote in message
news:gyYec.1179$(E-Mail Removed)
> 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)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why cant I use an aspx page class as TypeName for ObjectDataSource? yanni ASP .Net 1 11-09-2008 02:48 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Q: typename or not typename? Jakob Bieling C++ 2 03-14-2006 03:44 PM
Why typename can not be inherented from base class? PengYu.UT@gmail.com C++ 6 10-20-2005 03:52 PM
Why can't I use vector<T1>::size_type where T1 is a template typename? Eric Lilja C++ 3 04-23-2005 01:25 PM



Advertisments