Solved

Templatized defines...

Posted on 2001-06-14
7
146 Views
Last Modified: 2010-04-02
I am writing a large project. One of the namespaces is "Utility", where I'll have things like Vectors, Lists etc.
I use the STL, but want to get away from some of it's naming, so that should some of it's funtionality prove insufficient, I will replace the classes with my own implementations. I also want to restrict the template parameters for the STL containers in use, because I do my own memory management, and provide a class that is interface compatible with std::allocator.

0
Comment
Question by:Tom_P
  • 3
  • 3
7 Comments
 

Author Comment

by:Tom_P
ID: 6193746
So this is the scenario:
namespace Memory
{
 class MemoryManager {/*...*/}; //allocator compatible
}

Now I want to be able to do something like this:

namespace Utility
{
//this is the idea
  template<class T>
  typedef std::vector<T, Memory::MemoryManager<T> > Vector;
}

And then use the new type like, e.g.

Utility::Vector<int> v(/*which ever ctor*/);

Basically define a type that has one of the template arguments restricted, and hopefully rename the thing as well.
I don't think it is possible in C++, maybe just by deriving from std::vector and providing a similar set of ctors and a dtor. But should anyone have any bright ideas I will show appreciation accordingly.
0
 
LVL 22

Expert Comment

by:ambience
ID: 6193914
I used

template <class T>
class Vector : public std::vector<T,Memory::MemoryManager<T> > 
{
};

and this worked...

typedef std::vector<T, Memory::MemoryManager<T> > Vector;

doing that how would you supply the type T when you want to instantiate a Vector ??
I dont think this is possible , why ... well , i am not sure about that, i guess this has to do with the semantics  of typedefs , i'll be happy to know that if anyone could help here.

you cannot even try doing the following
typedef std::vector<T, Memory::MemoryManager<T> > Vector<T>;

as Vector<T> is not a valid typename.

0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 6194674
C++ doesn't allow templated typedefs, but there are workarounds.

Take a look at this link to a Guru of the Week:

http://www.gotw.ca/gotw/079.htm
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 9

Accepted Solution

by:
jasonclarke earned 200 total points
ID: 6194687
Basically, the GoTW solution would be this:

template <class T>
struct Vector
{
    typedef std::vector<T, Memory::MemoryManager<T> > Type;
};

then you could declare objects like this:

Vector<int>::Type v(...);

it may be a little less pretty than other things, but the meaning is identical.  The inheritance relationship is avoided.


0
 

Author Comment

by:Tom_P
ID: 6194753
Thank you Jason Clarke. That is what I thought but wasn't sure.
I think you deserve all the points, even just for the URL!
Thanks, Tom
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 6194787
The GotW stuff is excellent.  I would strongly encourage you (or anyone else for that matter) to read it if you are serious about C++.

The first 47 GotWs are expanded upon in a book, Exceptional C++, which is certainly on my list of required reading for C++.  Be warned though, if you think you know C++ quite well, it might be a humbling experience (you would be in good company though, Scott Meyers said more or less the same thing).
0
 

Author Comment

by:Tom_P
ID: 6194835
It is my plan to read all the articles. I already had a look. Yes and I do think I know C++ quite well... :) So I am in for a rough ride to prove me wrong (well hopefully not too wrong... :) ).
Anyway, thanks very much.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question