Hi, I've browsed the STL code a bit and stumble upon the next line (in
the operator << overload of both long and short outputs - the line is
for checking does the input is a manipulator)
long _Tmp = (_Bfl == ios_base::oct || _Bfl == ios_base::hex)
? (long)(unsigned short)_Val //<-This Line
: (long)_Val;
Is there any profound reason why they "Double" casting the _Val?
Thanks. 8 5465
On 30 mar, 10:06, ManicQin <Manic...@gmail.comwrote:
Hi, I've browsed the STL code a bit and stumble upon the next line (in
the operator << overload of both long and short outputs - the line is
for checking does the input is a manipulator)
long _Tmp = (_Bfl == ios_base::oct || _Bfl == ios_base::hex)
? (long)(unsigned short)_Val //<-This Line
: (long)_Val;
Is there any profound reason why they "Double" casting the _Val?
Not knowing the type of _Val, I can't say, but in general,
casting through an unsigned type guarantees a positive value in
the long (unless the positive value won't fit).
--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
ManicQin wrote:
Hi, I've browsed the STL code a bit and stumble upon the next line
(in the operator << overload of both long and short outputs - the
line is for checking does the input is a manipulator)
long _Tmp = (_Bfl == ios_base::oct || _Bfl == ios_base::hex)
? (long)(unsigned short)_Val //<-This Line
: (long)_Val;
Is there any profound reason why they "Double" casting the _Val?
Thanks.
Yes, the idea is to get the value unsigned, to avoid sign extension
for hex and octal representation.
Bo Persson
>Not knowing the type of _Val, I can't say, but in general,
>casting through an unsigned type guarantees a positive value in the long (unless the positive value won't fit).
The same behavior returns when _Val is long and short.
Yes, the idea is to get the value unsigned, to avoid sign extension
for hex and octal representation.
So why not just cast it directly to unsigned long... why the DOUBLE
cast...
I'm referring to the line (long)(unsigned short)_Val
besides ... when casting to unsigned and then to signed isn't the
result will be signed again?
(AFAIK long is synonymous with signed long int)
ManicQin wrote:
>Not knowing the type of _Val, I can't say, but in general, casting through an unsigned type guarantees a positive value in the long (unless the positive value won't fit).
The same behavior returns when _Val is long and short.
>Yes, the idea is to get the value unsigned, to avoid sign extension for hex and octal representation.
So why not just cast it directly to unsigned long... why the DOUBLE
cast...
I'm referring to the line (long)(unsigned short)_Val
besides ... when casting to unsigned and then to signed isn't the
result will be signed again?
It will not be the same if the short value is negative.
Bo Persson
ManicQin <Ma******@gmail.comwrote:
Hi, I've browsed the STL code a bit and stumble upon the next line (in
the operator << overload of both long and short outputs - the line is
for checking does the input is a manipulator)
long _Tmp = (_Bfl == ios_base::oct || _Bfl == ios_base::hex)
? (long)(unsigned short)_Val //<-This Line
: (long)_Val;
Is there any profound reason why they "Double" casting the _Val?
Thanks.
Let's pretend that _Val is a short and sizeof(short) is 2 and
sizeof(long) is 4...
If _Bfl == oct or hex, and _Val == 0xFFFF (-1), then _Val will be cast
to (unsigned short)0xFFFF (65535) and then to (long)0xFFFF (65535).
whereas if _Bfl is not oct or hex and _Val == 0xFFFF (-1), then _Val
will be cast to (long)0xFFFFFFFF (-1).
As you can see, the double cast is important if you want to maintain the
literal bit pattern, but the single cast is important if you want to
maintain the logical decimal value.
On Mar 30, 7:06*pm, "Daniel T." <danie...@earthlink.netwrote:
ManicQin <Manic...@gmail.comwrote:
Hi, I've browsed the STL code a bit and stumble upon the next line (in
the operator << overload of both long and short outputs - the line is
for checking does the input is a manipulator)
long _Tmp = (_Bfl == ios_base::oct || _Bfl == ios_base::hex)
* * * * * * * * * * * * * * * * ? (long)(unsigned short)_Val //<-This Line
* * * * * * * * * * * * * * * * *: (long)_Val;
Is there any profound reason why they "Double" casting the _Val?
Thanks.
Let's pretend that _Val is a short and sizeof(short) is 2 and
sizeof(long) is 4...
If _Bfl == oct or hex, and _Val == 0xFFFF (-1), then _Val will be cast
to (unsigned short)0xFFFF (65535) and then to (long)0xFFFF (65535).
whereas if _Bfl is not oct or hex and _Val == 0xFFFF (-1), then _Val
will be cast to (long)0xFFFFFFFF (-1).
As you can see, the double cast is important if you want to maintain the
literal bit pattern, but the single cast is important if you want to
maintain the logical decimal value.
mmm... and that's why the else statement ":" returns just long to
maintain the value...
Ok thanks!
On Mar 31, 8:53 am, ManicQin <Manic...@gmail.comwrote:
On Mar 30, 7:06 pm, "Daniel T." <danie...@earthlink.netwrote:
ManicQin <Manic...@gmail.comwrote:
Hi, I've browsed the STL code a bit and stumble upon the next line (in
the operator << overload of both long and short outputs - the line is
for checking does the input is a manipulator)
long _Tmp = (_Bfl == ios_base::oct || _Bfl == ios_base::hex)
? (long)(unsigned short)_Val //<-This Line
: (long)_Val;
Is there any profound reason why they "Double" casting the _Val?
Thanks.
Let's pretend that _Val is a short and sizeof(short) is 2 and
sizeof(long) is 4...
If _Bfl == oct or hex, and _Val == 0xFFFF (-1), then _Val will becast
to (unsigned short)0xFFFF (65535) and then to (long)0xFFFF (65535).
whereas if _Bfl is not oct or hex and _Val == 0xFFFF (-1), then _Val
will becastto (long)0xFFFFFFFF (-1).
As you can see, the doublecastis important if you want to maintain the
literal bit pattern, but the singlecastis important if you want to
maintain the logical decimal value.
mmm... and that's why the else statement ":" returns just long to
maintain the value...
Ok thanks!
Just wondering, is there an obvious reason why STL doesn't use the C++
cast operators? In this case I believe the appropriate one would be
static_cast<>.
T
On 31 Mar, 22:50, tommy.hi...@gmail.com wrote:
[snip]
Just wondering, is there an obvious reason why STL doesn't use the C++
cast operators? In this case I believe the appropriate one would be
static_cast<>.
I'd just like to point out that the example you refer to is from an
iostreams implementation, which is not part of the STL. It could be
that this code was written before the "new" cast operators became
widely used, or maybe this particular implementor prefers the C-style
cast.
I could be wrong, but I do believe the original STL implementation
predates the new cast operators. However, there are certainly STL
implementations that uses them. (SGI, for example).
DP This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Aaron W. West |
last post by:
Fun with CAST! (Optimized SQLServerCentral script posts)
I found some interesting "tricks" to convert binary to hexadecimal and back,
which allow doing 4 or 8 at a time.
Test code first:
--...
|
by: Michael Baehr |
last post by:
I recently upgraded my Arch Linux system to GCC 3.4, and found out that a
previously accepted behavior (cast-as-lvalue) is now marked as deprecated,
and will cease to function in GCC 3.5. This has...
|
by: Jim Hubbard |
last post by:
http://www.eweek.com/article2/0,1759,1774642,00.asp
|
by: TS |
last post by:
Hi, i have inherited a page from another user. The page is the target frame
in a frameset. for some reason the whole page runs twice (page_load, init,
etc. all run twice)
I can't figure out how...
|
by: grid |
last post by:
Hi,
I have a certain situation where a particular piece of code works on a
particular compiler but fails on another proprietary compiler.It seems
to have been fixed but I just want to confirm if...
|
by: fniles |
last post by:
I am accessing MS Access db with tblA whose column UNABLE can have NULL
value. When I access UNABLE whose value is null, I got an error "
Run-time exception thrown : System.InvalidCastException -...
|
by: Jack |
last post by:
Is it possible to cast a 4-byte data type (e.g. an unsigned int) to a
4-byte struct? Sometimes the HIWORD and LOWORD of a 4-byte value
contain information independent of each other. Is there a...
|
by: arindam.mukerjee |
last post by:
I was running code like:
#include <stdio.h>
int main()
{
printf("%f\n", 9/5);
return 0;
}
|
by: Arnie |
last post by:
Folks,
We ran into a pretty significant performance penalty when casting floats.
We've identified a code workaround that we wanted to pass along but also was
wondering if others had experience...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |