Click here to Skip to main content
11,505,123 members (68,900 online)
Click here to Skip to main content

Tagged as

C++ Numeric Limits and Template Specialization

, 3 Mar 2014 CPOL 4.8K 3
Rate this:
Please Sign up or sign in to vote.
C++ Numeric Limits and Template Specialization

Introduction

The purpose of this article is to explain the usage of Template Specialization with an example. It requires users to have some basic understanding on C++ programming & templates.

Template

C++ allows users to write generalized code using templates. What it means is that if you have a class like this...

class myData {
    int i;
    public:
    void printData() { std::cout<<i<<"\n"; return; }
    void setData(const int &value) { i = value; return; } 
};   

You can generalize it in the following manner...

template <class T> class myData {
    T i;
    public:
    void printData() { std::cout<<i<<"\n"; return; }
    void setData(const T &value) { i = value; return; } 
};   

and then use it with your desired type on a need basis.

 myData<int> obj;
 
 obj.setData(5);
 obj.printData();  

This will generate the following output:

 5 

Numeric Limits

Numeric Limits is another C++ feature which is not known to everyone, and can be useful in many cases. Defined in "limits" , and part of "std" namespace, it provides you an Interface to find various limits of different types.

What it means is that if you want to know what is the maximum value an int can support... you can do this:

 std::cout<<"Maximum Value of integer can take is ="<<std::numeric_limits<int>::max()<<std::endl;  

And it will print the following...

Maximum Value of integer can take is = 2147483647  

Interface is quite powerful... and has most of the limits and other stuff defined. IT can also tell you if one type is signed or not. I recommend having a look at the "limits" class for more insight.

Template Specialization for Numeric Type

Problem with numeric_limit is that it works only with basic type. If it is user defined type.. then we cannot use numeric_type for them. For example, if I have a class like this...

 class soldier {
    //...
    int _age;
    //... 
}    

We can't do this... your code will not compile.

 std::numeric_limits<soldier>::max(); 

Here, we can use Template Specialization to define limits for us. C++ allows you to define special cases for your templates... so if we take our first example (i.e. myData), we can specialize it for one type (let's say bool) in the following way:

template <> class myData<bool> {
    bool i;
    public:
    void printData() { i?std::cout<<"true\n":std::cout<<"false\n"; return; }
    void setData(const bool &value) { i = value; return; } 
}; 

And now, we can use it this way...

myData<bool> flag;
 
flag.setData(true);
flag.printData(); 

I am skipping the output part.. as it is quite obvious.

Now we can use template specialization to define max for soldier class. Let's say definition of max is "Maximum Age" and let's also take some value (say 50).

What we need to do it specialize numeric_limits class to support our type. Something like the following...

 namespace std {
        template<> class numeric_limits<soldier> {
           public:
          static int max() {return 50;};
        };
} 

Now you can use numeric_limits for your type ... in the following way:

 std::cout<<"Maximum age for a soldier is "<<std::numeric_limits<soldier>::max()<<"\n"; 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

vikrantc1355
Software Developer Yahoo
India India
No Biography provided
Follow on   Twitter

Comments and Discussions

 
Suggestionstd::numeric_limits must not be extended Pin
.:floyd:.4-Mar-14 10:27
member.:floyd:.4-Mar-14 10:27 
GeneralRe: std::numeric_limits must not be extended Pin
vikrantc13554-Mar-14 14:41
membervikrantc13554-Mar-14 14:41 
GeneralRe: std::numeric_limits must not be extended Pin
.:floyd:.4-Mar-14 15:46
member.:floyd:.4-Mar-14 15:46 
GeneralRe: std::numeric_limits must not be extended Pin
vikrantc13554-Mar-14 16:04
membervikrantc13554-Mar-14 16:04 
GeneralRe: std::numeric_limits must not be extended Pin
.:floyd:.5-Mar-14 4:49
member.:floyd:.5-Mar-14 4:49 
GeneralRe: std::numeric_limits must not be extended Pin
vikrantc13555-Mar-14 7:42
membervikrantc13555-Mar-14 7:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 4 Mar 2014
Article Copyright 2014 by vikrantc1355
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid