|
-
October 8th, 2005, 01:47 PM
#1
Limit the type's used in a templated class.
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.
-
October 8th, 2005, 02:40 PM
#2
Re: Limit the type's used in a templated class.
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.
Code:
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;
}
Har Har
-
October 8th, 2005, 03:27 PM
#3
Re: Limit the type's used in a templated class.
Concepts are a more general way of doing what you want. Boost has a concept library (http://www.boost.org/libs/concept_ch...cept_check.htm) that you can look at to understand the concept behind concepts.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|