CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2005
    Posts
    3

    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.

  2. #2
    Join Date
    Aug 2002
    Location
    Cluj-Napoca,Romania
    Posts
    3,496

    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

  3. #3
    Join Date
    Feb 2003
    Posts
    377

    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
  •  





Click Here to Expand Forum to Full Width

Featured