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;
}