Solved

Templatized defines...

Posted on 2001-06-14
7
150 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

630 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