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::string, 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::string)
{
T* pT = NULL;
std::map<std::string, T*>::iterator iTptr = m_objectMap.find();
if(iTptr != m_objectMap.end())
pT = *iTptr;
return pT;
}
private:
std::map<std::string, 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 3 2382
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::string, 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::string) { T* pT = NULL; std::map<std::string, T*>::iterator iTptr = m_objectMap.find();
typename std::map<...>::iterator iTptr = ...
^^^^^^^^
if(iTptr != m_objectMap.end()) pT = *iTptr; return pT; } private: std::map<std::string, 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
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::string, 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::string) { T* pT = NULL; std::map<std::string, T*>::iterator iTptr = m_objectMap.find();
Make this
typename std::map<std::string, T*>::iterator iTptr =
m_objectMap.find();
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
* 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::string, 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::string) { T* pT = NULL; std::map<std::string, T*>::iterator iTptr = m_objectMap.find(); if(iTptr != m_objectMap.end()) pT = *iTptr; return pT; } private: std::map<std::string, 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? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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>()
|
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...
|
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...
|
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...
|
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.
...
| |
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) ;
|
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...
|
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));...
|
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>...
|
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...
|
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,...
| |
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...
|
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,...
|
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...
|
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...
|
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: 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...
| |
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |