A
Angel Tsankov
What is a standard way to get the "length in bits" (in the sense of
5.8/1) of a type?
5.8/1) of a type?
Hi,Angel Tsankov said:What is a standard way to get the "length in bits" (in the sense of
5.8/1) of a type?
Francis Glassborow said:I think there is a misunderstanding here. Fundamental types must use all
the bits in their value representation but that is not the same as the
amount of storage required. I.e. it is perfectly possible to have
64-bits of storage (8 octets) for an int but only use 32 bits for the
value representation.
* Matthias Hofmann, on 29.04.2011 02:55:
Right.
The standard mentions two's complement, one's complement and signed and
magnitude as possible representations.
Essentially the shift operators impose additional requirements
that rule out more esoteric representations (like gray code
).
I don't think that was intentional; rather, that the goal was just
to allow the binary representations in use in the 1970's.
IMHO opinion it would have been nice if we could just ditch
the "ENIAC-support":
like, require two's complement, require well-defined operator
expressions, so on. But this has been discussed repeatedly and
at length before. I think C++0x was the point of final freeze
of adult features in C++, so to speak: since the change didn't
happen with C++0x it will never happen, so we're stuck with
the annoying behaviors along with the nicer ones... :-(
The problem for the OP is that whilst we can easily evaluate
the number of bits in a type's object representation I know of
no way to evaluate (you can look it up in the documentation)
the number of bits in the value representation.
James Kanze said:Not for plain char. In C++. (This is, I think, a difference
between C and C++. C allows trapping bit patterns in plain
char. Not that it makes a difference in practice.)
Newsbeitragnews:ac3bc85f-aa5d-465b-b0c5-83ab1fac78fa@x18g2000yqe.googlegroups.com...
Yes, it does make a difference.
Assuming that the standard allows a conversion from a pointer
to any object type to void* and back to char*, the following
two utility functions are legal:
// Converts a pointer of any non-const
// type to a non-const char pointer.
inline char* char_ptr( void* p ) throw()
{ return static_cast<char*>( p ); }
// Converts a pointer of any constant
// type to a constant char pointer.
inline const char* char_ptr( const void* p ) throw()
{ return static_cast<const char*>( p ); }
But what if the value of one byte accessed through such a char
pointer is a trap representation? Then iterating through the
bytes of the underlying object may cause the program to crash!
The answer seems to be using a pointer to unsigned char
instead of plain char, but there is a problem with that, too:
3.9.2/4 guarantees a char* to have the same representation as
a void*, but it does not give such a guarantee to unsigned
char*.
So how do you implement these two utility functions above?
If you you a plain char*, then you may have problems with bit
patterns or sign expansion errors, and if you use an unsigned
char, then you have object representation problems.
But if C++ does in fact not allow trapping bit patterns, then
the problem is solved and plain char should be used! So could
you please refer me to the corresponding section of the
standard where I can find such a guarantee?
Bart van Ingen Schenau said:No, but the >> operator is not defined if the left-hand operand has a
value
< 0.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.