Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    cpp
  » Template Specialization
      by Scott Deming
 Page 3 of 3 
< Previous    

(Login to remove green text ads)
A complete (small) and useful program
The following program uses the techniques described in this short article to assist in porting applications from a 32-bit to 64-bit platform. To see for yourself, copy, compile, and run the program.

A complete program:
Code:
/**
 * Quick and dirty program to display sizeof's, min's, and max's of various
 * data types.  Useful data when porting from 32 bit to 64 bit code.
*/

#include <iostream>
#include <iomanip>
#include <limits>

#include <string>
#include <vector>

using namespace std;

/// typedef some common types for clarity.
typedef unsigned char      UCHAR;
typedef unsigned short     USHORT;
typedef unsigned int       UINT;
typedef unsigned long      ULONG;
typedef long long          LLONG;
typedef unsigned long long ULLONG;

/// base template free-function for writing out the sizeof, min, and max
/// of the specified type.
template<typename T>
void print(T zero, const char* name)
{
  cout << name << ": s(" << sizeof(T) << ") ["
       << setw(20) << numeric_limits<T>::min() << " : "
       << setw(20) << numeric_limits<T>::max() << "]"
       << endl;
}

/// specialization for char.
///
/// since ostream will try to display the actual character represented by
/// the value, we must cast it as an int on output.
template<>
void print<char>(char zero, const char* name)
{
  cout << name << ": s(" << sizeof(char) << ") ["
       << setw(20) << static_cast<int>(numeric_limits<char>::min()) << " : "
       << setw(20) << static_cast<int>(numeric_limits<char>::max()) << "]"
       << endl;
}

/// specialization for unsigned char
///
/// since ostream will try to display the actual character represented by
/// the value, we must cast it as an int on output.
template<>
void print<UCHAR>(UCHAR zero, const char* name)
{
  cout << name << ": s(" << sizeof(UCHAR) << ") ["
       << setw(20) << static_cast<UINT>(numeric_limits<UCHAR>::min()) << " : "
       << setw(20) << static_cast<UINT>(numeric_limits<UCHAR>::max()) << "]"
       << endl;
}

int main()
{
  print(static_cast<void*>(0),  "void*               ");
  print(size_t(0),              "size_t              ");
  print(string::size_type(0),   "string::size_type   ");
  print(vector<void*>::size_type(0), "vector::size_type   ");

  print(char(0),                "char                ");
  print(UCHAR(0),               "unsigned char       ");

  print(short(0),               "short               ");
  print(USHORT(0),              "unsigned short      ");

  print(int(0),                 "int                 ");
  print(UINT(0),                "unsigned int        ");

  print(long(0),                "long                ");
  print(ULONG(0),               "unsigned long       ");

  print(LLONG(0),               "long long           ");
  print(ULLONG(0),              "unsigned long long  ");

  print(float(0),               "float               ");
  print(double(0),              "double              ");


  return 0;
}
32-bit output
Code:
void*               : s(4) [                   0 :                    0]
size_t              : s(4) [                   0 :           4294967295]
string::size_type   : s(4) [                   0 :           4294967295]
vector::size_type   : s(4) [                   0 :           4294967295]
char                : s(1) [                -128 :                  127]
unsigned char       : s(1) [                   0 :                  255]
short               : s(2) [              -32768 :                32767]
unsigned short      : s(2) [                   0 :                65535]
int                 : s(4) [         -2147483648 :           2147483647]
unsigned int        : s(4) [                   0 :           4294967295]
long                : s(4) [         -2147483648 :           2147483647]
unsigned long       : s(4) [                   0 :           4294967295]
long long           : s(8) [-9223372036854775808 :  9223372036854775807]
unsigned long long  : s(8) [                   0 : 18446744073709551615]
float               : s(4) [         1.17549e-38 :          3.40282e+38]
double              : s(8) [        2.22507e-308 :         1.79769e+308]
64-bit output
Code:
void*               : s(8) [                   0 :                    0]
size_t              : s(8) [                   0 : 18446744073709551615]
string::size_type   : s(8) [                   0 : 18446744073709551615]
vector::size_type   : s(8) [                   0 : 18446744073709551615]
char                : s(1) [                -128 :                  127]
unsigned char       : s(1) [                   0 :                  255]
short               : s(2) [              -32768 :                32767]
unsigned short      : s(2) [                   0 :                65535]
int                 : s(4) [         -2147483648 :           2147483647]
unsigned int        : s(4) [                   0 :           4294967295]
long                : s(8) [-9223372036854775808 :  9223372036854775807]
unsigned long       : s(8) [                   0 : 18446744073709551615]
long long           : s(8) [-9223372036854775808 :  9223372036854775807]
unsigned long long  : s(8) [                   0 : 18446744073709551615]
float               : s(4) [         1.17549e-38 :          3.40282e+38]
double              : s(8) [        2.22507e-308 :         1.79769e+308]





 
 Page 3 of 3 
< Previous    

Rate This Article
1 2 3 4 5 6 7 8 9 10





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting