Thread: Numeric Templates

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    329

    Numeric Templates

    Hi,

    When designing template classes that take only numeric types, such as int/double, is there a specific way to allow only these two types (for instance), or when using a template is it up to the programmer to be aware of what types are allowed?

    Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I believe that the answer is yes (offhand: you specialise the class template for those types, but leave it with say, a static assertion otherwise), but generally you would not want to do this since if the type behaves like an int or a double for your purposes, you would want to allow it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by laserlight View Post
    I believe that the answer is yes (offhand: you specialise the class template for those types, but leave it with say, a static assertion otherwise), but generally you would not want to do this since if the type behaves like an int or a double for your purposes, you would want to allow it.
    Sorry, you have completely lost me! I am only just starting to read up on templates.
    So, for example, I have an exercise in my book where I create a template Number<T> where T can be any numeric type. Is there a method to restrict types to only those that are numeric? If so, what do I need to read up on?

    Thanks again.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes. For example:
    Code:
    template<typename T> class MyClass
    {
        static_assert(false, "Type must be int.");
    };
    
    template<> class MyClass<int>
    {
        // Your class here
    };
    But keep in mind that you shouldn't restrict types like this unless you absolutely need it. For example, if your code doesn't--or cannot--handle certain types, you can ban them or allow only specific ones.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Okay thanks. That's pretty much answered it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple vector class problem, please help
    By fidodidohk in forum C++ Programming
    Replies: 9
    Last Post: 03-30-2007, 09:13 AM
  2. Questions about Templates
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 12-18-2005, 12:22 AM
  3. only accept numeric data
    By willc0de4food in forum Windows Programming
    Replies: 6
    Last Post: 08-19-2005, 03:38 AM
  4. templates and inheritance problem
    By kuhnmi in forum C++ Programming
    Replies: 4
    Last Post: 06-14-2004, 02:46 AM