Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Templatized defines...

Posted on 2001-06-14
7
Medium Priority
?
152 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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…
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 learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

722 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