Recursive partial specialization

D

daniel.w.gelder

Hi. I have a template with 3 params:

template <typename BASE, typename SUPER=void, typename SUPER2=void>
Coolness
{
};

typedef Coolness<long> LongCoolness;
typedef Coolness<long, bool> LongAndBoolCoolness;

OK, this is the trick here. When the template is typedefed in this
form:

typedef Coolness<BASE, Coolness<T1, T2>, void> Thing;

IE with a Coolness as a second parameter, I then want T1 and T2 to be
the parameters instead:

typedef Coolness<BASE, T1, T2> Thing;

Can I accomplish with partial specialization?

Thanks very much
Dan
 
K

Kanenas

Hi. I have a template with 3 params:

template <typename BASE, typename SUPER=void, typename SUPER2=void>

Don't forget a 'struct' or 'class' keyword.
Coolness
{
};

typedef Coolness<long> LongCoolness;
typedef Coolness<long, bool> LongAndBoolCoolness;

OK, this is the trick here. When the template is typedefed in this
form:

typedef Coolness<BASE, Coolness<T1, T2>, void> Thing;

IE with a Coolness as a second parameter, I then want T1 and T2 to be
the parameters instead:

typedef Coolness<BASE, T1, T2> Thing;

Can I accomplish with partial specialization?
You can't get type equivalence, but you can close if you also use
inheritance.

template <BASE, typename T1, typename T2>
class Coolness<BASE, Coolness<T1, T2>, void>
: public Coolness<BASE, T1, T2>
{
typedef Coolness<T1, T2> Super1;
typedef Coolness<BASE, T1, T2> Base;

/* These constructors may or may not make any sense
for Coolness.
*/
Coolness(const BASE& b, const T1& t1, const T2& t2)
: Base(b, t1, t2) {}
Coolness(const BASE& b, const Super1& s)
};

Any template instantiations which have a 1 or 2 arg Coolness as the
second parameter, e.g. 'Coolness<string, Coolness<Coolness<string>,
string> >', will use the specialization. Note that the inner
Coolness-es in the example ('Coolness<string>' and
'Coolness<Coolness<string>, long>') do not use the specialization.
However, 'Coolness<string, Coolness<string, Coolness<string> > >' has
2 Coolness-es which do use the specialization.

Kanenas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,204
Messages
2,571,065
Members
47,672
Latest member
svaraho

Latest Threads

Top