Compiler Quirk or Language flaw?

S

Steve

Hello I was compiling a program, and noticed something that seems
incredibly odd to me, and am wondering if this is a "Feature" from the
compiler or possibly there are in fact 2 different datatypes with the
same name, under the ANSI standard.

Here is an example.

bool MyFunction(int MyVar);

BOOL MyFunction(int MyVar){
if(MyVar <=5){
return(TRUE);
}else{
return(FALSE);
}

The problem I am having is that BOOL and bool appear to my compiler to
be different datatypes, I am using VS.NET 2003.

If I change case on either one it works fine as long as the case
matches.
I realize C/C++ are case sensitive, but I would expect one or the
other to simply cause the compiler to kick out with "Undefined
Datatype "BOOL" on line 5" or something along those lines, instead it
complains that I am overloading and the only difference is in the
return type.

Can someone please explain what is going on here?
 
H

Howard

Steve said:
Hello I was compiling a program, and noticed something that seems
incredibly odd to me, and am wondering if this is a "Feature" from the
compiler or possibly there are in fact 2 different datatypes with the
same name, under the ANSI standard.

Here is an example.

bool MyFunction(int MyVar);

BOOL MyFunction(int MyVar){
if(MyVar <=5){
return(TRUE);
}else{
return(FALSE);
}

The problem I am having is that BOOL and bool appear to my compiler to
be different datatypes, I am using VS.NET 2003.

If I change case on either one it works fine as long as the case
matches.
I realize C/C++ are case sensitive, but I would expect one or the
other to simply cause the compiler to kick out with "Undefined
Datatype "BOOL" on line 5" or something along those lines, instead it
complains that I am overloading and the only difference is in the
return type.

Can someone please explain what is going on here?

The bool type is part of the standard. The BOOL datatype is "user-defined",
or in your case, more specifically "implementation defined", in that some
part of your compiler's libraries probably defines the type BOOL. It may
defined it as bool or as unsigned int (or anything else they wanted), but in
any case it's a different type, and so you're getting expected behavior.
Same as if you used unsigned int versus bool.

-Howard
 
R

red floyd

Howard said:
The bool type is part of the standard. The BOOL datatype is "user-defined",
or in your case, more specifically "implementation defined", in that some
part of your compiler's libraries probably defines the type BOOL. It may
defined it as bool or as unsigned int (or anything else they wanted), but in
any case it's a different type, and so you're getting expected behavior.
Same as if you used unsigned int versus bool.

-Howard
Microsoft defines BOOL as

typedef int BOOL;

bool is defined by the Holy Standard. BOOL is defined by Microsoft.
 
T

Tim Slattery

The problem I am having is that BOOL and bool appear to my compiler to
be different datatypes, I am using VS.NET 2003.

They are different datatypes. "bool" is a primitive data type defined
by the C++ standard, as are the constants "true" and "false". "BOOL"
is defined someplace in the complex of files that windows.h pulls in
to be an int. The values "TRUE" and "FALSE" are defined the same place
to be used with it. BOOL goes back to the early days of the Windows
SDK, before C++ and the "bool" datatype existed. There was a need for
a boolean datatype, and this is how MS dealt with it. BOOL is still
heavily used by the Windows API functions.
 
J

John Harrison

Steve said:
Hello I was compiling a program, and noticed something that seems
incredibly odd to me, and am wondering if this is a "Feature" from the
compiler or possibly there are in fact 2 different datatypes with the
same name, under the ANSI standard.

Here is an example.

bool MyFunction(int MyVar);

BOOL MyFunction(int MyVar){
if(MyVar <=5){
return(TRUE);
}else{
return(FALSE);
}

The problem I am having is that BOOL and bool appear to my compiler to
be different datatypes, I am using VS.NET 2003.

What gave you the idea that BOOL is part of any standard? BOOL is
non-standard as are TRUE and FALSE.
If I change case on either one it works fine as long as the case
matches.
I realize C/C++ are case sensitive, but I would expect one or the
other to simply cause the compiler to kick out with "Undefined
Datatype "BOOL" on line 5" or something along those lines, instead it
complains that I am overloading and the only difference is in the
return type.

Can someone please explain what is going on here?

Microsoft typedef BOOL as int, which is obviously different from bool. Don't
use non-standard code when you don't have to.

john
 
A

Andrey Tarasevich

Steve said:
Hello I was compiling a program, and noticed something that seems
incredibly odd to me, and am wondering if this is a "Feature" from the
compiler or possibly there are in fact 2 different datatypes with the
same name, under the ANSI standard.

Here is an example.

bool MyFunction(int MyVar);

BOOL MyFunction(int MyVar){
if(MyVar <=5){
return(TRUE);
}else{
return(FALSE);
}

The problem I am having is that BOOL and bool appear to my compiler to
be different datatypes, I am using VS.NET 2003.

If I change case on either one it works fine as long as the case
matches.
I realize C/C++ are case sensitive, but I would expect one or the
other to simply cause the compiler to kick out with "Undefined
Datatype "BOOL" on line 5" or something along those lines, instead it
complains that I am overloading and the only difference is in the
return type.
...

There's no such thing as 'BOOL' in C++. If your compiler recognizes this
type in your code, it must be user-defined in some vendor-specific
library header file. It won't be surprising if it is defined to be
something else than 'bool'.
 
I

Ioannis Vranos

Steve said:
Hello I was compiling a program, and noticed something that seems
incredibly odd to me, and am wondering if this is a "Feature" from the
compiler or possibly there are in fact 2 different datatypes with the
same name, under the ANSI standard.

Here is an example.

bool MyFunction(int MyVar);

BOOL MyFunction(int MyVar){
if(MyVar <=5){
return(TRUE);
}else{
return(FALSE);
}

The problem I am having is that BOOL and bool appear to my compiler to
be different datatypes, I am using VS.NET 2003.


Doesn't VS include a help file?


Platform SDK: Windows API

....


ATOM Atom. For more information, see Atoms.
BOOL Boolean variable (should be TRUE or FALSE).
BOOLEAN Boolean variable (should be TRUE or FALSE).




It is Win32 type, not .NET. Also as it is known C++ distinguishes between
uppercase and lowercase, so BOOL, BoOl, Bool, bool etc are different
identifiers. The ISO C++ type is bool which is the case of .NET is also the
..NET boolean type. (.NET types map directly to the default types of the
various supported languages, where the are equivalent default types of
course).






Ioannis Vranos
 
S

Steve

What gave you the idea that BOOL is part of any standard? BOOL is
non-standard as are TRUE and FALSE.
Microsoft typedef BOOL as int, which is obviously different from bool. Don't
use non-standard code when you don't have to.

john

So how SHOULD this have been written, so that it's fully compatible
with the current standard?
 
J

John Harrison

Steve said:
So how SHOULD this have been written, so that it's fully compatible
with the current standard?

bool MyFunction(int MyVar);

bool MyFunction(int MyVar){
if(MyVar <=5){
return(true);
}else{
return(false);
}

or even more simply

bool MyFunction(int MyVar){
return MyVar <= 5;
}

john
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top