Partial template specialization (ignore: daft mistake)

Started by
1 comment, last by TheUnbeliever 16 years, 8 months ago
Which of the following is better, and why? Specialized template inheriting from more generic template

template<bool NeedsY = false, typename X = std::string, typename Y = int> class foo
{
    X bar;
};

template<typename X, typename Y> class foo<true, X, Y> : foo<false, X, Y>
{
    Y baz;
};
Specialized template friend of more generic template

template<bool NeedsY = false, typename X = std::string, typename Y = int> class foo
{
    X bar;
    friend class foo<true, X, Y>;
};

template<typename X, typename Y> class foo<true, X, Y> : foo<false, X, Y>
{
    Y baz;
};
[Actual code in second reply] I'm inclined to think that the former is the better option, as foo<true, Y, Z> is a refinement that adds functionality to foo<false, Y, Z> but isn't a different 'type' (type being used in the conceptual manner, I realise that foo<false, Y, Z> and foo<true, Y, Z> are different types from the point of view of the language). On the other hand, is this just a bad idea? If so, should I just copy-and-paste (ick) the contents of foo<false, Y, Z> into the body of foo<true, Y, Z> or should I dump the partial specialization and have an entirely separate templated class which inherits from foo<Y, Z> (no need for the first bool template argument)? I'm personally opposed to both of these: the first is ugly and the second for the reasons given above, but I'm willing to be told I'm wrong. [grin] EDIT: Also, I've realised (must have been getting a bit carried away) that the friend option doesn't actually do what I want, and is obviously not equivalent to the inheritance option, so please discount that. I guess I'm basically now asking whether there is any reason that I shouldn't go with the inheritance design. [Edited by - TheUnbeliever on August 14, 2007 2:48:29 PM]
[TheUnbeliever]
Advertisement
I think both options are illegal!

In the first, I do not believe that you can have a default parameter preceding a mandatory parameter (unless I've missed something...).

In the second, you can't have friends that depend on template parameters to your template class. <OT>This one is a pain because if it were legal, we could implement "final" classes as found in Java.</OT>

As for giving advice, I for one would really need to see what the classes were for i.e. what kind of larger problem are you attempting to solve?

Edd
Quote:Original post by the_edd
I think both options are illegal!

In the first, I do not believe that you can have a default parameter preceding a mandatory parameter (unless I've missed something...).


You're right. It was my mistake in reducing the code to an example (EDIT: OP code now changed). All the parameters are optional, as below.

Quote:As for giving advice, I for one would really need to see what the classes were for i.e. what kind of larger problem are you attempting to solve?


I've realised (must have been getting a bit carried away) that the friend option doesn't actually do what I want, and is obviously not equivalent to the inheritance option, so please discount that. I guess I'm basically now asking whether there is any reason that I shouldn't go with the inheritance design.

[Edited by - TheUnbeliever on August 14, 2007 2:31:55 PM]
[TheUnbeliever]

This topic is closed to new replies.

Advertisement