473,566 Members | 2,924 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

template-nested STL iterator question

I'm curious about what appears to be a restriction on using an STL
container inside a user-defined template, esp. using an iterator to
such a container. It's not clear to me if this is a general
template/language restriction, and STL iterator limitation, or if I'm
just going about it wrong.

I'm declaring a template which uses a std::map to store references to
the template type, e.g.

template template <typename T>
class MyClass
{
public:
// ...
private:
std::map<std::s tring, T*> m_objectMap;
};

This compiles fine. However if I have a declaration of an iterator to
this map in a method of my class, the compile fails, e.g.

template template <typename T>
class MyClass
{
public:
T* Lookup(std::str ing)
{
T* pT = NULL;
std::map<std::s tring, T*>::iterator iTptr = m_objectMap.fin d();
if(iTptr != m_objectMap.end ())
pT = *iTptr;
return pT;
}
private:
std::map<std::s tring, T*> m_objectMap;
};

Is there a correct way to accomplish this? If not, anyone know the
rationale?

The kludgy workaround I'm using is to use void * instead of T* for the
std::map and static-cast it.

- Chris

May 10 '06 #1
3 2392
chriscorbell wrote:
I'm curious about what appears to be a restriction on using an STL
container inside a user-defined template, esp. using an iterator to
such a container. It's not clear to me if this is a general
template/language restriction, and STL iterator limitation, or if I'm
just going about it wrong.

I'm declaring a template which uses a std::map to store references to
the template type, e.g.

template template <typename T>
class MyClass
{
public:
// ...
private:
std::map<std::s tring, T*> m_objectMap;
};

This compiles fine. However if I have a declaration of an iterator to
this map in a method of my class, the compile fails, e.g.

template template <typename T>
class MyClass
{
public:
T* Lookup(std::str ing)
{
T* pT = NULL;
std::map<std::s tring, T*>::iterator iTptr = m_objectMap.fin d();
typename std::map<...>:: iterator iTptr = ...
^^^^^^^^
if(iTptr != m_objectMap.end ())
pT = *iTptr;
return pT;
}
private:
std::map<std::s tring, T*> m_objectMap;
};

Is there a correct way to accomplish this? If not, anyone know the
rationale?
Read the FAQ. Dependent names are described in the 'Templates' section.
The kludgy workaround I'm using is to use void * instead of T* for the
std::map and static-cast it.


That's a BAD IDEA(tm).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 10 '06 #2

chriscorbell wrote:
I'm curious about what appears to be a restriction on using an STL
container inside a user-defined template, esp. using an iterator to
such a container. It's not clear to me if this is a general
template/language restriction, and STL iterator limitation, or if I'm
just going about it wrong.

I'm declaring a template which uses a std::map to store references to
the template type, e.g.

template template <typename T>
class MyClass
{
public:
// ...
private:
std::map<std::s tring, T*> m_objectMap;
};

This compiles fine. However if I have a declaration of an iterator to
this map in a method of my class, the compile fails, e.g.

template template <typename T>
class MyClass
{
public:
T* Lookup(std::str ing)
{
T* pT = NULL;
std::map<std::s tring, T*>::iterator iTptr = m_objectMap.fin d();


Make this

typename std::map<std::s tring, T*>::iterator iTptr =
m_objectMap.fin d();

Because T is a template argument, the compiler has no way, when it
parses this statement, to know what it is. That means it cannot know
what "iterator" is in this context (std::map could be specialized for a
given T and that specialization could have no member "iterator" or
"iterator" could even be an object and not a type). By adding
"typename", you specify the compiler that "iterator" is a type name
inside std::map. It'll double check later.
Jonathan

May 10 '06 #3
* chriscorbell:
I'm curious about what appears to be a restriction on using an STL
container inside a user-defined template, esp. using an iterator to
such a container. It's not clear to me if this is a general
template/language restriction, and STL iterator limitation, or if I'm
just going about it wrong.

I'm declaring a template which uses a std::map to store references to
the template type, e.g.

template template <typename T>
class MyClass
{
public:
// ...
private:
std::map<std::s tring, T*> m_objectMap;
};

This compiles fine.
I get a syntax error on the first line.

Which compiler is it that accepts the above?

However if I have a declaration of an iterator to
this map in a method of my class, the compile fails, e.g.

template template <typename T>
class MyClass
{
public:
T* Lookup(std::str ing)
{
T* pT = NULL;
std::map<std::s tring, T*>::iterator iTptr = m_objectMap.fin d();
if(iTptr != m_objectMap.end ())
pT = *iTptr;
return pT;
}
private:
std::map<std::s tring, T*> m_objectMap;
};
Curiously, as long as the Lookup function isn't actualy called, with the
first line corrected this (incorrectly) compiles fine with MSVC 7.1.

Is there a correct way to accomplish this?
Presumably you intended to (1) pass that string argument as 'std::string
const& s', (2) supply some argument to 'find', e.g. '.find(s)', and (3)
access the second field of the found pair, 'pT = iTptr->second'.

If not, anyone know the rationale?
For what?

The kludgy workaround I'm using is to use void * instead of T* for the
std::map and static-cast it.


That shouldn't work.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 10 '06 #4

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

Similar topics

4
3911
by: Sebastian Faust | last post by:
Hi, I have 4 questions related to templates. I wanna do something like the following: template<typename T> class Template { public: Template_Test<T>()
1
3327
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me to turn a previously made String class that deals with char's into a templated String class that uses the template parameter C instead of char. I...
31
3456
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 return. I tried to write something like this: template <class Type1, class Type2, class Type3> Type3 findMin(Type1 x, Type2 y){ return (x < y) ? x...
5
2166
by: Gianni Mariani | last post by:
The spirit of this arguably pointless exercise, is that the numeric_limits<T> class could be replaced with a totally generic template of compile-time, template computed constants. The problem is that it takes a *long* time to compile and the "long double" version instantiates so many classes that gcc runs out of memory on my 1 gig machine !...
2
2005
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. This makes it difficult to spot errors. For example, F4 to "jump to next error" just jumps ot the next "template assistance" message. And since...
2
4499
by: Alfonso Morra | last post by:
I have a class declared as ff: class __declspec(dllexport) A { public: A() ; A(const A&) A& operator=(const A&) ; ~A() ; void doThis(void) ;
3
3745
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 thawed it out. I built a console app using Microsoft Visual C++ 6 (VC++) and it worked great. Only one line in the header file had to be commented out....
9
3467
by: Leo jay | last post by:
i'd like to implement a class template to convert binary numbers to decimal at compile time. and my test cases are: BOOST_STATIC_ASSERT((bin<1111,1111,1111,1111>::value == 65535)); BOOST_STATIC_ASSERT((bin<1111>::value == 15)); BOOST_STATIC_ASSERT((bin<0>::value == 0)); BOOST_STATIC_ASSERT((bin<1010, 0011>::value == 163)); you can find...
2
2196
by: Gary Nastrasio | last post by:
I'm currently reading Andrei Alexandrescu's book "Modern C++ Design" and I'm a bit confused by one bit of template syntax in chapter 1. Here is a code example: template <class CreationPolicy> class WidgetManager : public Creation Policy {...} // Create an instance of WidgetManager which managers type Widget WidgetManager<...
1
4454
by: matz2k | last post by:
I've got a big problem with the CSS layout which I've produced with Photoshop/Dreamweaver especially for my ebay auctions. This is what it looks like http://cgi.ebay.de/ws/eBayISAPI.dll?ViewItem&item=250542673235 As you can see it can only be seen partially , can anyone help me with this? As fas as I can tell ebay's Iframe is messing up my...
0
7893
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8109
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7953
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6263
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5485
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1202
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.