Specifying Allowed Template Classes

This is a discussion on Specifying Allowed Template Classes within the C++ Programming forums, part of the General Programming Boards category; Is there any way to specify classes you would allow a template to be used for? For example, I want ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    18

    Question Specifying Allowed Template Classes

    Is there any way to specify classes you would allow a template to be used for?

    For example, I want to allow about 3 (int, float, char) data types to be used for this one class, as what it does would be identical for all those types, so naturally I think template, however, I don't want other data types, like user defined classes to be passed in that would potentially cause errors.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You can create a template on generic type T that causes a compile error. Then specialize the template for float, int and char so that it doesn't cause a compile error.

    Alexandrescu has more information in his Modern C++ Design book and Loki library.

    Also, template specialization is discussed here: http://www.parashift.com/c++-faq-lit....html#faq-35.7 and in the next couple entries.

    You could use boost's StaticAssert library to force the compiler error. http://boost.org/doc/html/boost_staticassert.html (see the examples at the bottom).

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by nine-hundred View Post
    Is there any way to specify classes you would allow a template to be used for?

    For example, I want to allow about 3 (int, float, char) data types to be used for this one class, as what it does would be identical for all those types, so naturally I think template, however, I don't want other data types, like user defined classes to be passed in that would potentially cause errors.
    Look at boost::enable_if

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,301
    If the template wont compile for some classes then that will already stop people from being able to use your template with those classes.

    On the other hand, if it does work then who are you to say that the user of the template shouldn't be allowed to use it like that?
    If the reason for this is that your class makes certain assumptions, that might compile fine but just not work, then the correct thing to do is to add some compile-time assertions to cover those assumptions. Look into C_ASSERT.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. More explicit template member function hell
    By SevenThunders in forum C++ Programming
    Replies: 1
    Last Post: 03-17-2009, 10:36 PM
  2. Templates and Macros plus more...
    By Monkeymagic in forum C++ Programming
    Replies: 8
    Last Post: 01-20-2007, 04:53 PM
  3. help with template class using a template node
    By aciarlillo in forum C++ Programming
    Replies: 11
    Last Post: 06-02-2005, 05:46 PM
  4. Operator overloading in template classes
    By moejams in forum C++ Programming
    Replies: 5
    Last Post: 07-21-2003, 05:16 PM
  5. Template Classes
    By AdioKIP in forum C++ Programming
    Replies: 5
    Last Post: 02-24-2002, 05:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21