Sign in to follow this  

Partial template specialization (ignore: daft mistake)

This topic is 3773 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

This topic is 3773 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this