Robopanda333
Robopanda333

Reputation: 25

Template specialization for recursive type

I am working on creating an instrumentation class for a large code repository, to that end i created a class like the following:

template <class T> class stats:public singleton<stats<T> >{...};

The singleton class is defined as you would expect from the above, and is not a class i have control over. Because i only want one stat instance per type i am gathering stats on it should be a singleton, and in our code base we forbid custom singleton classes (otherwise i would implement my own singleton interface and thus prevent the problem i am facing) The problem is that i want to tool the singleton as well, so the following will occur:

someTooledClass();
-> stats<someTooledClass>
-> singleton<stats<someTooledClass> >
-> stats<singleton<stats<someTooledClass> > >
etc.

My question is can i specialize stats<singleton<T> >? Because T is unknown i am unsure how this would even work. Ideally i wouldn't want stats<singleton<stats<T> > > to ever exist but all other stats<singleton<T> > i do want. I just have never really done anything like this before... so i'm a bit confused.

Edit: I guess i'll put this here for visibility reasons. Tooling singleton is possible because i am globally overriding new and delete to my own custom ones for all classes in the build, as singleton is in the build path (indeed it must be) i can tool it in this way. For background the task i was given is to create a memory manager so i know what i am getting into with that (i think... but that is really a separate issue anyway). but for now i am trying to gather statistics on all currently used memory allocations segregated by what type, not just by size class.

Upvotes: 0

Views: 142

Answers (1)

Ben Voigt
Ben Voigt

Reputation: 283883

Did you try it?

Note that you can't make the class not exist, however you can make it not inherit from singleton, in order to break the chain.

template <class T>
class stats<singleton<stats<T> > > {};

Upvotes: 1

Related Questions