Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Templatized defines...

Posted on 2001-06-14
7
Medium Priority
?
153 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Accepted Solution

by:
jasonclarke earned 800 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

876 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