-
-
Notifications
You must be signed in to change notification settings - Fork 7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
boolean data type causes logic errors. #2147
Comments
I will post this as a pull request soon, with the additional info for the defines true and false. |
In C, the bool type is also available through including I would welcome a change to more standard typings, but some thought has to be given to compatility: are there real cases where a sketch would break with this change? |
I have just made a pull request. #2151 However with true and false defined as 0 & 1 this sketch fails. Stdbool fixes that, but not the fact it actually defines a keyword, so its not needed. My fix is safe as it is using the real deal in C++, and C sees it how it was once defined. This fails for instance in 1.5.6 void setup() {
Serial.begin( 9600 );
DoSpecific( true );
DoSpecific( false );
DoSpecific( 1 );
DoSpecific( 0 );
}
void loop(){}
void DoSpecific( bool value ){
Serial.print( "The boolean value is: " );
Serial.println( value ? "TRUE" : "FALSE" );
}
void DoSpecific( int value ){
Serial.print( "The integer value is: " );
Serial.println( value, HEX );
} |
Why is it a problem that stdbool "defines a keyword" exactly? |
Its not a problem, its just not allowed
Also it is not needed, And yes the |
Fixed with 20ac20f |
Match current Arduino definition to avoid issues with comparison operations. arduino/Arduino#2147 arduino/Arduino@20ac20f Fixes esp8266#5440
Match current Arduino definition to avoid issues with comparison operations. arduino/Arduino#2147 arduino/Arduino@20ac20f Fixes #5440
It has come to my attention that the
boolean
type alias produces results differing to the C++ standard boolean typebool
.Consider this sketch:
Using this sketch with
boolean
declared as atypedef
touint8_t
( which is what the current core does ) the output to the serial monitor is:Whereas, after changing the core to allow a
boolean
to be declared asbool
. The sketch produces this output:It appears
boolean
has been implemented to provide boolean functionality with some C code in the Arduino core. I recommend changing the core so by the timeboolean
is seen by a sketch it is either atypedef
or#define
to the realbool
type.These are the modifications I propose be implemented in Arduino.h
On a side note, the defines for
true
andfalse
should be removed. Boolean's ( the real one ) are subject to integral promotion and convert to either 0 or 1. Also the standard forbids macros named the same as keywords. If the C code needs defines, useTRUE
&FALSE
instead.I have created a pull request here:
#2151
I also have a proof I'm in the middle of writing as to why the defines
true
andfalse
need to be removed from the C++ side. I will add it soon.The text was updated successfully, but these errors were encountered: