the difrence betwen true and TRUE
As the tile says I would want to know what''s the difrence betwen "true" and "TRUE" in C++ ( and offcourse "false" and "FALSE" are analog I think ). I use VC++ 6.0
Well I''ve noticed that true is an int var and TRUE is a bool var but beside that is there any real difrence ?
Thanks
DariusKing
true is a bool constant, built into the C++ language.
TRUE is a #defined int constant used by windows APIs, with a value of 1
While it is true that all non-zero integral values evaluate to the boolean true and that true can be converted to the integral value 1, the two are not equivalent (I had a code snippet showing that, but I can't remember it).
The use of true is preferable in any case. TRUE is a hack that hails back to C (which didn't have a bool type until the C99 Standard) and pre-standard C++. It has no place in modern code... except maybe for backward compatibility with legacy APIs.
Here's an example:
If you pass true, it will print "Equal", but not if you pass TRUE. In the first case, 42 is converted into a bool and thus is evaluated as true, while in the second case, it stays an int and, well, 1 != 42.
There are other tricks, playing on the fact that it is the bool that gets converted into an int when checking whether they are equal, and other mixed-type comparisons.
The sizes are also not guaranteed to be the same: sizeof( true ) may be different from sizeof( TRUE ).
Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
[edited by - Fruny on August 31, 2002 3:19:41 PM]
TRUE is a #defined int constant used by windows APIs, with a value of 1
While it is true that all non-zero integral values evaluate to the boolean true and that true can be converted to the integral value 1, the two are not equivalent (I had a code snippet showing that, but I can't remember it).
The use of true is preferable in any case. TRUE is a hack that hails back to C (which didn't have a bool type until the C99 Standard) and pre-standard C++. It has no place in modern code... except maybe for backward compatibility with legacy APIs.
Here's an example:
template<class T> void Foo( T f ){ if( f == (T)42 ) cout << "Equal" << endl;}
If you pass true, it will print "Equal", but not if you pass TRUE. In the first case, 42 is converted into a bool and thus is evaluated as true, while in the second case, it stays an int and, well, 1 != 42.
There are other tricks, playing on the fact that it is the bool that gets converted into an int when checking whether they are equal, and other mixed-type comparisons.
The sizes are also not guaranteed to be the same: sizeof( true ) may be different from sizeof( TRUE ).
Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
[edited by - Fruny on August 31, 2002 3:19:41 PM]
I usualy use true too but today I was coding a litle MFC application and I got a worning when I tryed to compile this piece of code :
// bla bla... other things
if(m_bMesaj == true )
{
// other stuff here
}
the worning was :
"warning C4805: ''=='' : unsafe mix of type ''int'' and type ''const bool'' in operation "
and then I replaced true with TRUE and the code compilded without any wornings.....
So that''s way I''ve posted here....
DariusKing
// bla bla... other things
if(m_bMesaj == true )
{
// other stuff here
}
the worning was :
"warning C4805: ''=='' : unsafe mix of type ''int'' and type ''const bool'' in operation "
and then I replaced true with TRUE and the code compilded without any wornings.....
So that''s way I''ve posted here....
DariusKing
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement