Angus Comber
Angus Comber

Reputation: 9708

Why does Visual C++ 2008 not complain when I (accidentally) use type boolean

I have been doing some Java work and so mistakenly in a cpp file created a function like this:

inline boolean is_whatever() const { return type_ == whatever; }

And it compiled just fine.

Then when I compiled the same code on UNIX it failed with:

error: `boolean' does not name a type

Why doesn't VS complain?

Is there a flag that I can set to prevent this laxity?

Upvotes: 1

Views: 188

Answers (3)

the_mandrill
the_mandrill

Reputation: 30842

It just so happens that boolean has been defined in some other header that you have included (directly or indirectly). For instance if you include windows.h then that pulls in many other headers, some of which have this:

typedef unsigned char boolean;

If any of those headers are included in your cpp file then boolean becomes part of the namespace. If this was to be 'fixed' in the Windows API then thousands of legacy projects would no longer compile and developers would be up in arms. It's use probably dates back to before bool was a concrete type in C++ (bear in mind too that the Windows API is C, rather than C++)

Upvotes: 1

tenfour
tenfour

Reputation: 36896

boolean is not a C++ type. Maybe there is a typedef bool boolean somewhere, but it's not a built-in type.

Use bool instead.

Upvotes: 0

Eduard Rostomyan
Eduard Rostomyan

Reputation: 6556

Use bool instead of boolean, the you will avoid such inconveniences.

Upvotes: 1

Related Questions