Numeric Templates

This is a discussion on Numeric Templates within the C++ Programming forums, part of the General Programming Boards category; Hi, When designing template classes that take only numeric types, such as int/double, is there a specific way to allow ...

  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
    21,709
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,583
    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-17-2005, 11:22 PM
  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

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