Nested Template Issue

Started by
2 comments, last by Mr Awesome 18 years ago
Here is some code to demonstrate what I'm trying to do:

template<size_t Size, class Type, class A>
class SomePolicy;

template
<
    size_t Size,
    class Type,

    template
    <size_t, class, class A>
    class Policy,
>
class Group
{
    Policy<Size, ParticleType, A> Instance;
};

I'm trying to accept a class template as a template parameter, then sort of partially specialize in the body of the class. Obviously this produces an error because you can't use a named template parameter from the definition, but I'm trying to avoid having to do something like this: Group<100, Thing, SomePolicy<100, Thing, Action> > someGroup; That will become not only tedious but error-prone as the number of policies increase and become further nested. So, does anyone know of a way to get around this problem, or am I basically stuck retyping Size and Type for each policy? Thanks in advance.
Advertisement
I hope this will help you:

/* my dummy definitions here... */class ParticleType {};class Thing {};class Action {};/* your policy template */template<size_t Size, class Type, class A>class SomePolicy {};/* your group template */template<    size_t Size,    class Type,    class A, /* ADDED the action as a template parameter */    template    <size_t, class, class>    class Policy /* REMOVED the comma... */>class Group{    Policy<Size, ParticleType, A> Instance;};/*a specific instanciation exemple*/Group<100, Thing, Action, SomePolicy> someGroup;


I really like the idea of a templated particle system. Templates are so usefull.
Currently, I'm building a templated rendering engine.
Your code and example are somewhat contradictory. Did you mean to have ParticleType just be Type (the type parameter of Group).

Quote:Original post by Mr Awesome
I'm trying to accept a class template as a template parameter, then sort of partially specialize in the body of the class. Obviously this produces an error because you can't use a named template parameter from the definition, but I'm trying to avoid having to do something like this:

What do you mean? You want to partially specialize the passed in template from within Group? That can't be done and I don't see why you would ever want to. If not, could you rephrase what you are trying to say, because I think you may be misunderstanding some terms -- I can't figure out exactly what you are trying to express. A better example showing both how you want to instantiate the template and exactly what behavior you expect would help.

Quote:Original post by Mr Awesome
Group<100, Thing, SomePolicy<100, Thing, Action> > someGroup;


Do you just want to be able to do

Group< 100, Thing, SomePolicy > someGroup;

and have someGroup contain a SomePolicy< 100, Thing, Action > as a datamember? If so, then just do:

template< ::std::size_t Size, typename Type, typename A >class SomePolicy;template<  ::std::size_t Size, typename Type, template< ::std::size_t, typename, typename >  class Policy>class Group{    Policy< Size, Type, Action > Instance;};Group< 100, int, SomePolicy > someGroup;


It's hard to tell exactly what you want without a better explanation. Where is Action coming from, where is ParticleType coming from?
Sorry I didn't explain too well, but you sort of got what I was going for. There were some typos in there (forgot to erase the Particle in ParticleType), but you corrected them already. The SomePolicy class needs the same Size and Type parameters as Group, but I also want to specify another template parameter from the global scope:

Group<100, SomeType, OuterPolicy</*100, SomeType,*/ InnerPolicy> > theGroup;

Basically, I want to have the Group template pass the Size and Type parameters to the OuterPolicies, while still allowing me to pass in additional InnerPolicies to the OuterPolicies. That sounds confusing, but hopefully the above example shows what I'm trying to do. I may end up having to pass InnerPolicies as parameters directly to Group, which can then pass that to the OuterPolicy, as skalco mentioned.

Group<100, SomeType, OuterPolicy, InnerPolicy> theGroup;
// Translates roughly to
Group<100, SomeType, OuterPolicy<100, SomeType, InnerPolicy> > theGroup;
// and is much safer.

I'm following a particle engine tutorial on this site, but I'm trying to refine it so it isn't quite so tedious.

This topic is closed to new replies.

Advertisement