Solved

Templatized defines...

Posted on 2001-06-14
7
141 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now