Click here to Skip to main content
15,895,799 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Correct usage of reinterpret_cast and static_cast Pin
John R. Shaw12-May-07 10:58
John R. Shaw12-May-07 10:58 
AnswerRe: Correct usage of reinterpret_cast and static_cast Pin
bob1697212-May-07 12:06
bob1697212-May-07 12:06 
GeneralRe: Correct usage of reinterpret_cast and static_cast Pin
John R. Shaw12-May-07 12:36
John R. Shaw12-May-07 12:36 
GeneralRe: Correct usage of reinterpret_cast and static_cast Pin
bob1697212-May-07 13:20
bob1697212-May-07 13:20 
GeneralRe: Correct usage of reinterpret_cast and static_cast Pin
Stephen Hewitt13-May-07 15:00
Stephen Hewitt13-May-07 15:00 
GeneralRe: Correct usage of reinterpret_cast and static_cast Pin
John R. Shaw14-May-07 15:12
John R. Shaw14-May-07 15:12 
GeneralRe: Correct usage of reinterpret_cast and static_cast Pin
ComplexLifeForm12-May-07 21:11
ComplexLifeForm12-May-07 21:11 
GeneralRe: Correct usage of reinterpret_cast and static_cast Pin
bob1697213-May-07 2:07
bob1697213-May-07 2:07 
Fair enough.
I'm not going to claim to know why but I'll take a guess at it.

In VC++ 4.2 on up (likely compiler specific),
BOOL will always maintain it's integral value while bool only allows 2 values for it's type. For instance...

BOOL BOOLVal=5;
TRACE("BOOLVal(5) = %d\n",BOOLVal); // 0 = FALSE. All other values = TRUE
// outputs 5

bool boolVal=(bool)-42;
TRACE("boolVal(-42) = %d\n",boolVal); // Always 0 or 1 (false or true)
// outputs 1

A cast (as we've come to expect it) from BOOL to bool in this case should still truncate the integral value and maintain the bits for the first 8 bits but since bool only allows true and false as 1 and 0, an additional conversion, besides the cast must take place. I'm guessing that's where this operation is no longer a cast and becomes a translation of the value.

// Assembly for a BOOL=bool
119: BOOL isCorrect = 56;
00401973 mov dword ptr [ebp-1Ch],38h
120: bool bvalue = isCorrect;
0040197A xor edx,edx
0040197C cmp dword ptr [ebp-1Ch],0
00401980 setne dl
00401983 mov byte ptr [ebp-20h],dl

// Assembly for a BOOL=BOOL
119: BOOL isCorrect = 56;
00401973 mov dword ptr [ebp-1Ch],38h
120: BOOL bvalue = isCorrect;
0040197A mov edx,dword ptr [ebp-1Ch]
0040197D mov dword ptr [ebp-20h],edx

/*
The setne instruction is "Set byte if not equal (ZF=0)" and it's description says "Sets the destination operand to 0 or 1 depending on the settings of the status flags
(CF, SF, OF, ZF, and PF) in the EFLAGS register"
*/

This is where I'm guessing the translation occurs versus a true cast and probably explains why the compiler takes the liberty to provide warnings. Also, it appears that there is a little history to bool in VC++ changing around version 4.2 and I would guess the warning is to give programmers with code written prior to the version 4.2 change an extra heads up to a potential "breaking change".

Not sure if I got that right but it seems reasonable. Smile | :)
AnswerRe: Correct usage of reinterpret_cast and static_cast Pin
Michael Dunn13-May-07 6:24
sitebuilderMichael Dunn13-May-07 6:24 
GeneralRe: Correct usage of reinterpret_cast and static_cast Pin
Stephen Hewitt13-May-07 14:31
Stephen Hewitt13-May-07 14:31 
GeneralRe: Correct usage of reinterpret_cast and static_cast Pin
bob1697213-May-07 19:41
bob1697213-May-07 19:41 
GeneralRe: Correct usage of reinterpret_cast and static_cast Pin
Stephen Hewitt13-May-07 19:48
Stephen Hewitt13-May-07 19:48 
GeneralRe: Correct usage of reinterpret_cast and static_cast Pin
Stephen Hewitt13-May-07 16:46
Stephen Hewitt13-May-07 16:46 
AnswerRe: Correct usage of reinterpret_cast and static_cast Pin
Stephen Hewitt13-May-07 15:04
Stephen Hewitt13-May-07 15:04 
QuestionVS build/run problem. [modified] Pin
CodeGoose12-May-07 9:01
CodeGoose12-May-07 9:01 
AnswerRe: VS build/run problem. Pin
Hans Dietrich12-May-07 10:55
mentorHans Dietrich12-May-07 10:55 
GeneralRe: VS build/run problem. Pin
CodeGoose12-May-07 13:31
CodeGoose12-May-07 13:31 
AnswerRe: VS build/run problem. Pin
prasad_som12-May-07 19:33
prasad_som12-May-07 19:33 
AnswerRe: VS build/run problem. Pin
Gary R. Wheeler13-May-07 2:37
Gary R. Wheeler13-May-07 2:37 
Questionsimple question Pin
Dj_Lordas12-May-07 8:26
Dj_Lordas12-May-07 8:26 
AnswerRe: simple question Pin
biswajit nayak12-May-07 8:50
biswajit nayak12-May-07 8:50 
AnswerRe: simple question Pin
John R. Shaw12-May-07 11:27
John R. Shaw12-May-07 11:27 
Questionrelated to vc++ and xml Pin
biswajit nayak12-May-07 8:13
biswajit nayak12-May-07 8:13 
AnswerRe: related to vc++ and xml Pin
CPallini12-May-07 8:21
mveCPallini12-May-07 8:21 
AnswerRe: related to vc++ and xml Pin
Hamid_RT12-May-07 8:26
Hamid_RT12-May-07 8:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.