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]