473,406 Members | 2,843 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,406 software developers and data experts.

Usage of typename


Folks

I understand that the keyword typename is also used to indicate that a
dependant parameter is a type. However I am a little unsure why I need
typename in this following example:

template<typename K, typename V>
class MyMap
{
public:
typedef typename std::map<K, V>::iterator MyMap_Iterator;
};

why is a typename needed here? what does iterator have to do with the
template parameters K and V? Irrespective of what K and V are am I not
trying to just typedef std::map's iterator? What am I missing?

Sep 23 '06 #1
2 1890
Dilip wrote:
public:
typedef typename std::map<K, V>::iterator MyMap_Iterator;
};

why is a typename needed here? what does iterator have to do with the
template parameters K and V? Irrespective of what K and V are am I not
trying to just typedef std::map's iterator? What am I missing?
std::map is a template, not a class, the class that can have a type called
iterator is map <K,V>. And that class is dependent of K and V. For example,
imagine than std::map has an specialization for some concrete K and V you
use, and that specialization has no member called iterator, or has one that
is not a type. May seem strange in the std::map concrete case, but the
rules are generic, it will be very confusing to have different rules for
standard library templates and other templates.

--
Salu2
Sep 23 '06 #2
Dilip <rd*****@lycos.comwrote:
Folks

I understand that the keyword typename is also used to indicate that a
dependant parameter is a type. However I am a little unsure why I
need typename in this following example:

template<typename K, typename V>
class MyMap
{
public:
typedef typename std::map<K, V>::iterator MyMap_Iterator;
};

why is a typename needed here? what does iterator have to do with the
template parameters K and V? Irrespective of what K and V are am I
not trying to just typedef std::map's iterator? What am I missing?
Afaik, 'iterator' _could_ (just suppose you have not seen the class
definition, yet) also be a function or a static member variable. By
prepending 'typename' you tell the compiler, that 'iterator' really is a
type inside this specialization of the template, so it does not have to
look it up itself.

I am sure there is a more technical and more accurate explanation to
this, like a quote from the Standard. The above is what I use to
determine, if I need 'typename' or not. I got this explanation from this
newsgroup as well, btw. Try googling the groups for my name and
'typename' in the subject.

hth
--
jb

(reply address in rot13, unscramble first)
Sep 23 '06 #3

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

Similar topics

2
by: Alexander Stippler | last post by:
Hi, I'm not quite sure if I have to use the keyword template in a situation, where my compiler enforces it, though I think it is not neccessary. My question is: Who is wrong with respect to the...
9
by: christopher diggins | last post by:
I would like to survey how widespread the usage of smart pointers in C++ code is today. Any anecdotal experience about the frequency of usage of smart pointer for dynamic allocation in your own...
11
by: ma740988 | last post by:
I'm perusing a slide with roughly 12 bullets spread across 3 pages. Each bullet reflects 'advice'. I'm ok with all but 1 bullet, more specifically the bullet that states: " Avoid the STL unless...
3
by: Joseph Turian | last post by:
Let's say I have a vector of Base. I want to convert it to a vector of Derived, where Derived::Derived(const Base&); is defined. Is it possible to convert the vector of Base to a vector of...
1
by: ma740988 | last post by:
I'm wading my way through Josuttis template text. I'm having a hard time understanding some things. So given: template <class T> class generic_traits { public: typedef T value_type; };...
6
by: Hunk | last post by:
Hi I have a question on usage of forward declarations of templates. While searching through this group , people suggested using forward declarations and typedefs for templates as // in...
2
by: adrian.hawryluk | last post by:
Hi everyone, I've been using templates for a while, but I'm not at full power yet (knowledge=power) ;). Does anyone know where I can get information on this 'new' template usage? template<a...
51
by: muktipada | last post by:
Hello, As a C++ developer which one we should use for pointer assignment, NULL or 0. typedef DummyC DummyClass*; // in some header file. DummyC obj = NULL; if (obj == NULL) {
8
by: jacek.dziedzic | last post by:
Hi! I need to be able to track memory usage in a medium-sized application I'm developing. The only significant (memory-wise) non- local objects are of two types -- std::vector<and of a custom...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.