🔒 Locked
Quickie
Advertisement
Original Post
Which C++ header file contains the sizes (in bytes or bits) of the standard simple data types (char, short, int, long, float, double, long double)? I need this info to make a header file that will work with different compilers (a neat template class that could help remove unneccessary derefencing).
Will this work?
results are:
#include <iostream>
using namespace std;
int main()
{
cout << "int: " << sizeof(int) << endl;
cout << "bool: " << sizeof(bool) << endl;
cout << "long: " << sizeof(long) << endl;
cout << "short: " << sizeof(short) << endl;
cout << "char: " << sizeof(char) << endl;
cout << "long double: " << sizeof(long double) << endl;
return 0;
}
results are:
int: 4
bool: 1
long: 4
short: 2
char: 1
long double: 8
No, I need the values at compile-time so that the preprocessor can handle them. I know in climits there's all the constants I need for integers and chars, but I need some way to find out how many bits are in floats / doubles at COMPILE-TIME (and it varies with different compilers so I can't just assume a double is 8 bytes or anything like that).
[edited by - Brobanx on January 19, 2003 11:08:19 PM]
[edited by - Brobanx on January 19, 2003 11:08:19 PM]
Could this work?
[edited by - HunterTKilla on January 19, 2003 12:12:09 AM]
#define intSize sizeof(int)
#define longSobuleSize sizeof(long double)
...
[edited by - HunterTKilla on January 19, 2003 12:12:09 AM]
I''m confused as to why sizeof is no good here? It is the proper operator. I think that maybe you should read the first few chapters of your C++ book again Brobanx
.
well, in float.h FLT_MANT_DIG, DBL_MANT_DIG and LDBL_MANT_DIG are defined as the number of bits in the mantissa, and...err....guess the exponent 
DBL_MANT_DIG is defined as 53 (MSVC6, x86) even though there are actually 52 mantissa bits, so I'm presuming this includes the sign bit.
Perhaps you could assume that the size in bytes is the next multiple of 2.
e.g.
mantBits = DBL_MANT_DIG-1;
mantBytes = (mantBits+7)/8;
dblBytes = mantBytes+(mantBytes%2);
I know this is a VERY dodgy hack, and don't even know if it would even work on anything other than an x86, but it's a suggestion none-the-less.
Joanus D'Mentia
---------------
Serving Mars since 3069.
[edited by - joanusdmentia on January 20, 2003 7:39:45 AM]
DBL_MANT_DIG is defined as 53 (MSVC6, x86) even though there are actually 52 mantissa bits, so I'm presuming this includes the sign bit.
Perhaps you could assume that the size in bytes is the next multiple of 2.
e.g.
mantBits = DBL_MANT_DIG-1;
mantBytes = (mantBits+7)/8;
dblBytes = mantBytes+(mantBytes%2);
I know this is a VERY dodgy hack, and don't even know if it would even work on anything other than an x86, but it's a suggestion none-the-less.
Joanus D'Mentia
---------------
Serving Mars since 3069.
[edited by - joanusdmentia on January 20, 2003 7:39:45 AM]
Thank you SabreMan, I got about 5 responses from people that didn't know what the preprocessor was.
I wanted some way to check the size of types before compiling some template specializations, so that I could more efficiently pass parameters / const references in templatized classes (like my vector class) where I may not know the size of a parameter, and cannot judge whether a reference or a copy is more efficient.
ie...
However, it was much quicker when I just downloaded the Boost libraries and used boost::call_traits classes.
[edited by - Brobanx on January 20, 2003 12:23:37 AM]
I wanted some way to check the size of types before compiling some template specializations, so that I could more efficiently pass parameters / const references in templatized classes (like my vector class) where I may not know the size of a parameter, and cannot judge whether a reference or a copy is more efficient.
ie...
template<class T>
class const_reference {
T const& val;
... other code here ...
};
#if (NUMBYTES_DOUBLE < NUMBYTES_POINTER)
template <>
class const_reference<double> {
double val;
... other code here ...
};
#endif
However, it was much quicker when I just downloaded the Boost libraries and used boost::call_traits classes.
[edited by - Brobanx on January 20, 2003 12:23:37 AM]
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement