Re: Template question...
Andy <Andy@discussions.microsoft.com> wrote:
First off, I have a template defined something like,
template <int Count>struct ACollectionOfThings
{
// Definition of a collection that contains exactly Count things.
};
I would like to be able to stop people using this class with certain
values of Count, as an example I also have a template,
template <int Count>struct ASmallCollectionOfThings
{
// Definition of a collection that contains exactly Count things.
};
And I would like people to use the Small collection for values of
Count less than some number, so I would like a compile time error to
be issued when someone did,
CollectionOfThings<ASmallNumber> ACollection;
How about this:
// Base template
template <int count, bool small>
struct CollectionOfThingsImpl {
// Implement large collection
};
// Partial specialization for small counts
template <int count>
struct CollectionOfThingsImpl<count, true> {
// Implement small collection
};
template <int count>
struct CollectionOfThings :
public CollectionOfThingsImpl<count, (cout < threshold)> {
};
Now your users would just say
CollectionOfThings<anyNumber> c;
and the appropriate implementation would be chosen automatically.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925