Archived

This topic is now archived and is closed to further replies.

Brobanx

Quickie

Recommended Posts

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).

Share this post


Link to post
Share on other sites
Will this work?

  
#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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
quote:
Original post by Brobanx
The preprocessor doesn''t recognize sizeof.

Of course. The preprocessor is just like a text replace utility.

hmm... what kind of code construct you are trying to do?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
quote:
Original post by Brobanx
No, I need the values at compile-time so that the preprocessor can handle them.

The preprocessor runs *before* compile-time. That''s why it''s called a "pre" processor.

Share this post


Link to post
Share on other sites
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...


      
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]

Share this post


Link to post
Share on other sites