Click to See Complete Forum and Search --> : Limit the type's used in a templated class.


defcon
October 8th, 2005, 01:47 PM
Let’s say I have a linked list class, templated. How can I prevent the user of my class from using some types of data in the class, or, how can I only allow them to use some predefined types?

I do not have any code for this, as it is a general question.

Thanks for any help you can give.

PadexArt
October 8th, 2005, 02:40 PM
To prevent the template being instantiated with a specified class you could specialize it explicitelly and ensure the specialization is not instantiable. For a well defined list of classes this can work.



class A{};

class B{};

template <class T> class Tmpl
{
};

template <> class Tmpl<A>
{
private:
Tmpl(){}
~Tmpl(){}
};

int main(int argc, char* argv[])
{
Tmpl<int> ti;
Tmpl<A> ta; // fails as the constructor is private
Tmpl<B> tb;

return 0;
}

jlou
October 8th, 2005, 03:27 PM
Concepts are a more general way of doing what you want. Boost has a concept library (http://www.boost.org/libs/concept_check/concept_check.htm) that you can look at to understand the concept behind concepts.