References to templated classes

Started by
1 comment, last by ill 11 years, 11 months ago
Is there a way in C++ to not require some templates arguments to be there, especially when it's a pointer?

For example, here is some code:

template <typename T, typename Id, typename Container>
struct ResHelper {
//some code

Id m_resourceId;
ResourceManager<T, Id, Container> * m_manager;
RefCountPtrRoot<T, ResHelper<T, Id, Container> >* m_root;
};

template <typename T, typename Id = uint64_t, typename Container = std::map<Id, ResourceInfo> >
class ResourceManager {
//code goes here
};



I would really like to write it more like this:

template <typename Id>
struct ResHelper {
//some code

Id m_resourceId;
ResourceManager<?, Id, ?> * m_manager;
RefCountPtrRoot<?, ResHelper<Id> >* m_root;
};

template <typename T, typename Id = uint64_t, typename Container = std::map<Id, ResourceInfo> >
class ResourceManager {
//code goes here
};


Here I'm avoiding other template arguments because this is a struct that holds an id with some variable type.
The rest are just pointers. I put question marks in places where a type would normally go for that class.

So far the only way I can get my code to compile is to do it the way I did it above. This gets really annoying and messy when I have dependancies all over the place and end up needing classes that had very simple template arguments suddenly need like 6 template arguments because they have all these pointers.

I had a class before that contained a ResHelper<typename Id> but after doing some work now it needs to be ResHelper<typename T, typename Id, typename Container>. Now the templated class that contains the ResHelper<> needs to also provide template arguments for these extra things.
Advertisement
You could derive your ResourceManager from an interface type that's only templated on the Id and use pointers to that type.
I actually came up with an idea. The giant templated monster class will be internal. I'll make a wrapper around it that won't have all these templated arguments.

The giant templated class was supposed to be a kind of base class but I can just make wrapper classes that contain it. It'll also avoid virtual functions and things like that...

This topic is closed to new replies.

Advertisement